Algorithm


Problem Name: 1031. Maximum Sum of Two Non-Overlapping Subarrays

Given an integer array nums and two integers firstLen and secondLen, return the maximum sum of elements in two non-overlapping subarrays with lengths firstLen and secondLen.

The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping.

A subarray is a contiguous part of an array.

 

Example 1:

Input: nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
Output: 20
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.

Example 2:

Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
Output: 29
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.

Example 3:

Input: nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
Output: 31
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.

 

Constraints:

  • 1 <= firstLen, secondLen <= 1000
  • 2 <= firstLen + secondLen <= 1000
  • firstLen + secondLen <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const maxSumTwoNoOverlap = function(A, L, M) {
  for(let i = 1, len = A.length; i  <  len; i++) {
    A[i] += A[i - 1]
  }
  let LMax = A[L - 1], MMax = A[M - 1], res = A[L + M - 1]
  for(let i = L + M, len = A.length; i  <  len; i++) {
    LMax = Math.max(LMax, A[i - M] - A[i - M - L])
    MMax = Math.max(MMax, A[i - L] - A[i - M - L])
    res = Math.max(res, Math.max(LMax + A[i] - A[i - M], MMax + A[i] - A[i - L]))
  }
  return res
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2

Output

x
+
cmd
20

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def maxSumTwoNoOverlap(self, A: List[int], L: int, M: int) -> int:
        n = len(A)
        l = [0] * n
        r = [0] * n
        sm = 0 
        for i in range(M - 1):
            sm += A[i]
        for j in range(n - M + 1):
            sm += A[j + M - 1]
            for i in range(j + 1):
                r[i] = max(r[i], sm)
            sm -= A[j]
        
        sm = 0
        for i in range(n - 1, n - M, -1):
            sm += A[i]
        for i in range(n - 1, M - 2, -1):
            sm += A[i - M + 1]
            for j in range(i + 1, n):
                l[j] = max(l[j], sm)
            sm -= A[i]
            
        
        
        
        sm = 0
        for i in range(L - 1):
            sm += A[i]
        res = 0
        for j in range(n - L + 1):
            sm += A[j + L - 1]
            if j >= M:
                res = max(res, sm + l[j - 1])
            if j + L < n:
                res = max(res, sm + r[j + L])
            sm -= A[j]
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2

Output

x
+
cmd
29
Advertisements

Demonstration


Previous
#1030 Leetcode Matrix Cells in Distance Order Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1032 Leetcode Stream of Characters Solution in C, C++, Java, JavaScript, Python, C# Leetcode