Given an integer array nums and two integers firstLen and secondLen, find two non-overlapping subarrays that maximize their combined sum.
The first subarray must have exactly firstLen elements, and the second subarray must have exactly secondLen elements. These subarrays can appear in any order in the original array - the shorter one could come before or after the longer one.
Goal: Return the maximum possible sum of elements from both subarrays combined.
Example: For array [0,6,5,2,2,5,1,9,4] with firstLen=1 and secondLen=2, we could choose subarray [9] (length 1) and [6,5] (length 2) for a total sum of 9 + 11 = 20.
Input & Output
Visualization
Time & Space Complexity
O(n) to compute all subarray sums, O(nยฒ) to check all combinations
Storing all precomputed subarray sums
Constraints
- 1 โค firstLen, secondLen โค 1000
- 2 โค nums.length โค 1000
- firstLen + secondLen โค nums.length
- 0 โค nums[i] โค 1000
- The two subarrays must be non-overlapping