Maximum Sum of M Non-Overlapping Subarrays I Problem
Description
LeetCode Problem 3956.
You are given an integer array nums of length n, and three integers m, l, and r.
Your task is to select at least one and at most m non-overlapping subarrays from nums such that:
- Each selected subarray has a length between
[l, r](inclusive). - The total sum of all selected subarrays is maximized.
Return the maximum total sum you can achieve.
Example 1:
Input: nums = [4,1,-5,2], m = 2, l = 1, r = 3
Output: 7
Explanation:
One optimal strategy is to:
- Select the subarray [4, 1] with sum 4 + 1 = 5 and the subarray [2] with sum 2. Both subarrays have length between [l, r].
- The total sum of these subarrays is 5 + 2 = 7, which is the maximum achievable sum with at most m = 2 subarrays.
Example 2:
Input: nums = [1,0,3,4], m = 2, l = 1, r = 2
Output: 8
Explanation:
One optimal strategy is to:
- Select the subarray [1] with sum 1 and the subarray [3, 4] with sum 3 + 4 = 7. Both subarrays have length between [l, r].
- The total sum of these subarrays is 1 + 7 = 8, which is the maximum achievable sum with at most m = 2 subarrays.
Example 3:
Input: nums = [-1,7,-4], m = 1, l = 2, r = 3
Output: 6
Explanation:
- Select the subarray [-1, 7] from nums which has length between [l, r].
- The total sum of this subarray is -1 + 7 = 6, which is the maximum achievable sum with at most m = 1 subarray.
Example 4:
Input: nums = [-3,-4,-1], m = 2, l = 1, r = 2
Output: -1
Explanation:
- All subarrays of nums have negative sums. The optimal strategy is to select the subarray [-1], which has length between [l, r].
- The total sum of this subarray is -1, which is the maximum achievable sum with at most m = 2 subarrays.
Constraints:
1 <= n == nums.length <= 1000-10^9 <= nums[i] <= 10^91 <= m <= n1 <= l <= r <= n
Solution
Approach: Dynamic Programming with a Monotonic Queue
Let pre be the prefix sums and let f[j][i] be the best total of exactly j non-overlapping valid subarrays inside the first i elements (negative infinity if impossible, f[0][i] = 0). Either element i - 1 is unused, giving f[j][i - 1], or a subarray [t, i) with l <= i - t <= r ends there, giving f[j - 1][t] + pre[i] - pre[t].
For a fixed j the second option needs the maximum of f[j - 1][t] - pre[t] over a window of t that slides right as i grows, so a monotonic deque gives it in amortized O(1). The answer is the best f[j][n] for 1 <= j <= m; only two rows are kept at a time.
Complexity: time O(n * m), space O(n).
class Solution:
def maximumSum(self, nums: List[int], m: int, l: int, r: int) -> int:
n = len(nums)
pre = [0] * (n + 1)
for i, x in enumerate(nums):
pre[i + 1] = pre[i] + x
NEG = -(1 << 62)
prev = [0] * (n + 1) # prev[t]: best sum of j - 1 subarrays inside nums[:t]
ans = NEG
for j in range(1, m + 1):
cur = [NEG] * (n + 1)
qi = [0] * (n + 1) # monotonic queue of candidate start points t
qv = [0] * (n + 1)
head = tail = 0
for i in range(1, n + 1):
t = i - l
if t >= 0 and prev[t] != NEG:
v = prev[t] - pre[t]
while tail > head and qv[tail - 1] <= v:
tail -= 1
qi[tail] = t
qv[tail] = v
tail += 1
while tail > head and qi[head] < i - r:
head += 1
cur[i] = cur[i - 1]
if tail > head:
cur[i] = max(cur[i], qv[head] + pre[i])
ans = max(ans, cur[n])
prev = cur
return ans