Given an array nums of integers, find three non-overlapping subarrays with maximum sum.
Return the total sum of the three subarrays
Example:
Input: [2, 3, -8, 7, -2, 9, -9, 7, -2, 4] Output: 28 Explanation: Subarrays [2, 3], [7, -2, 9] and [7, -2, 4] have the maximum sum of 28
Note:
The core challenge of this problem is to find three non-overlapping subarrays that together have the maximum possible sum. This problem is significant in scenarios where we need to maximize the sum of multiple segments of data, such as in financial analysis or signal processing.
Potential pitfalls include overlapping subarrays and not considering all possible subarray combinations.
To solve this problem, we can break it down into smaller steps:
A naive solution would involve checking all possible combinations of three subarrays, but this approach is not optimal due to its high time complexity.
We can use dynamic programming to optimize the solution. The idea is to use three arrays to keep track of the best subarray sums up to each point in the array:
left
: Best subarray sum from the start to each point.right
: Best subarray sum from each point to the end.total
: Best total sum of three non-overlapping subarrays.Here is a step-by-step breakdown of the algorithm:
left
array with the best subarray sums from the start to each point.right
array with the best subarray sums from each point to the end.left
and right
arrays.def maxSumOfThreeSubarrays(nums, k):
n = len(nums)
sum_k = [0] * (n - k + 1)
current_sum = sum(nums[:k])
# Calculate the sum of each subarray of length k
for i in range(n - k + 1):
if i > 0:
current_sum = current_sum - nums[i - 1] + nums[i + k - 1]
sum_k[i] = current_sum
# Arrays to store the best subarray sums
left = [0] * len(sum_k)
right = [0] * len(sum_k)
# Fill the left array
best = 0
for i in range(len(sum_k)):
if sum_k[i] > sum_k[best]:
best = i
left[i] = best
# Fill the right array
best = len(sum_k) - 1
for i in range(len(sum_k) - 1, -1, -1):
if sum_k[i] >= sum_k[best]:
best = i
right[i] = best
# Find the maximum sum of three non-overlapping subarrays
max_sum = 0
for j in range(k, len(sum_k) - k):
i, l = left[j - k], right[j + k]
total = sum_k[i] + sum_k[j] + sum_k[l]
if total > max_sum:
max_sum = total
return max_sum
# Example usage
nums = [2, 3, -8, 7, -2, 9, -9, 7, -2, 4]
k = 2
print(maxSumOfThreeSubarrays(nums, k)) # Output: 28
The time complexity of this solution is O(n)
because we iterate through the array a constant number of times. The space complexity is also O(n)
due to the additional arrays used.
Potential edge cases include:
These cases are handled by the algorithm as it considers all possible subarray positions.
To test the solution comprehensively, consider the following test cases:
When approaching such problems, it is helpful to:
Understanding and solving problems like this one is crucial for developing strong algorithmic skills. Practice and exploration of different approaches can significantly enhance problem-solving abilities.
For further reading and practice, consider the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE