## # 1031. Maximum Sum of Two Non-Overlapping Subarrays

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarray.)

Formally, return the largest V for which `V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1])` and either:

`0 <= i < i + L - 1 < j < j + M - 1 < A.length`, or `0 <= j < j + M - 1 < i < i + L - 1 < A.length`.

Example 1:

``````Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
Output: 20
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.
``````

Example 2:

``````Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
Output: 29
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
``````

Example 3:

``````Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
Output: 31
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.
``````

### # Solution

Approach 1: brute force. Generate all subarray sums with lengths L and M, and pair them up if they don't overlap. O(N^2) time.

Approach 2: similar to Buy and Sell Stock III (twice), keep maximum sums of length L seen so far from the left, and max sum of length M from the right. Then find the split point i such that `left[i] + right[i]` is max. Do the same thing with M before L. O(N) time, O(N) space.

### # Code (Python)

Approach 1:

``````    def maxSumTwoNoOverlap(self, array: List[int], L: int, M: int) -> int:
sums_length_L = self._all_subarrays(L, array)
sums_length_M =self._all_subarrays(M, array)
result = 0
for l in sums_length_L:
for m in sums_length_M:
if self._no_overlap(l[1], L, m[1], M):
result = max(result, l[0] + m[0])
return result

def _all_subarrays(self, length, array):
# returns a list of (sum, start_index) pairs
total = sum(array[:length])
result = [(total, 0)]
for i in range(length, len(array)):
total = total + array[i] - array[i-length]
result.append((total, i - length + 1))
return result

def _no_overlap(self, start1, len1, start2, len2):
end1, end2 = start1 + len1 - 1, start2 + len2 - 1
return end1 < start2 or end2 < start1
``````

Approach 2:

``````    def maxSumTwoNoOverlap(self, array, L, M):
return max(self._max_sum(L, M, array), self._max_sum(M, L, array))

def _max_sum(self, len1, len2, array):
left, right = [-float('inf') for _ in range(len(array))], [-float('inf') for _ in range(len(array))]
total = sum(array[:len1])
for i in range(len1 - 1, len(array)):
left[i] = max(total, left[i-1]) if i != 0 else total
if i != len(array) - 1:
total = total + array[i+1] - array[i-len1+1]
total = sum(array[len(array)-len2:])
for j in range(len(array) - len2, -1, -1):
right[j] = max(total, right[i+1]) if i != len(array) - 1 else total
if j != 0:
total = total + array[j-1] - array[j+len2-1]
result = 0
for k in range(1, len(array)):
result = max(result, left[k-1] + right[k])
return result
``````

Approach 1:

Approach 2: