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
Inputnums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
Output20
One choice of subarrays is [9] with length 1, and [6,5] with length 2.
Example 2
Inputnums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
Output29
One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.

Constraints

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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate