Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums and two integers, k and m.
Return the maximum sum of k non-overlapping subarrays of nums, where each subarray has a length of at least m.
Example 1:
Input: nums = [1,2,-1,3,3,4], k = 2, m = 2
Output: 13
Explanation:
The optimal choice is:
nums[3..5] with sum 3 + 3 + 4 = 10 (length is 3 >= m).nums[0..1] with sum 1 + 2 = 3 (length is 2 >= m).The total sum is 10 + 3 = 13.
Example 2:
Input: nums = [-10,3,-1,-2], k = 4, m = 1
Output: -10
Explanation:
The optimal choice is choosing each element as a subarray. The output is (-10) + 3 + (-1) + (-2) = -10.
Constraints:
1 <= nums.length <= 2000-104 <= nums[i] <= 1041 <= k <= floor(nums.length / m)1 <= m <= 3Problem summary: You are given an integer array nums and two integers, k and m. Return the maximum sum of k non-overlapping subarrays of nums, where each subarray has a length of at least m.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,-1,3,3,4] 2 2
[-10,3,-1,-2] 4 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
class Solution {
public int maxSum(int[] nums, int k, int m) {
final int n = nums.length;
int[] prefix = new int[n + 1];
Integer[][][] mem = new Integer[n][2][k + 1];
for (int i = 0; i < n; i++)
prefix[i + 1] = prefix[i] + nums[i];
return maxSum(nums, 0, 0, k, m, prefix, mem);
}
private static final int INF = 20_000_000;
private int maxSum(int[] nums, int i, int ongoing, int k, int m, int[] prefix,
Integer[][][] mem) {
if (k < 0)
return -INF;
if (i == nums.length)
return k == 0 ? 0 : -INF;
if (mem[i][ongoing][k] != null)
return mem[i][ongoing][k];
if (ongoing == 1)
// 1. End the current subarray (transition to state 0, same index i)
// 2. Extend the current subarray by picking nums[i] and move to i + 1.
return mem[i][1][k] = Math.max(maxSum(nums, i, 0, k, m, prefix, mem),
maxSum(nums, i + 1, 1, k, m, prefix, mem) + nums[i]);
// ongoing == 0
// 1. Skip nums[i].
// 2. Pick nums[i..i + m - 1] (only if k > 0 and there're enough elements).
int res = maxSum(nums, i + 1, 0, k, m, prefix, mem);
if (i + m <= nums.length) // If we have enough elements for a new segment
res = Math.max(res,
maxSum(nums, i + m, 1, k - 1, m, prefix, mem) + (prefix[i + m] - prefix[i]));
return mem[i][0][k] = res;
}
}
// Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
package main
import "math"
// https://space.bilibili.com/206214
func maxSum(nums []int, k, m int) int {
n := len(nums)
s := make([]int, n+1)
for i, x := range nums {
s[i+1] = s[i] + x // nums 的前缀和
}
f := make([]int, n+1)
g := make([]int, n+1)
for i := 1; i <= k; i++ {
g[i*m-1] = math.MinInt
mx := math.MinInt
for j := i * m; j <= n-(k-i)*m; j++ {
mx = max(mx, f[j-m]-s[j-m])
g[j] = max(g[j-1], mx+s[j])
}
f, g = g, f
}
return f[n]
}
# Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
class Solution:
def maxSum(self, nums: list[int], k: int, m: int) -> int:
INF = 20_000_000
n = len(nums)
prefix = list(itertools.accumulate(nums, initial=0))
@functools.lru_cache(None)
def dp(i: int, ongoing: int, k: int) -> int:
if k < 0:
return -INF
if i == n:
return 0 if k == 0 else -INF
if ongoing == 1:
# 1. End the current subarray (transition to state 0, same index i)
# 2. Extend the current subarray by picking nums[i] and move to i + 1
return max(dp(i, 0, k),
dp(i + 1, 1, k) + nums[i])
# ongoing == 0
# 1. Skip nums[i]
# 2. Pick nums[i:i+m] (only if k > 0 and there're enough elements)
res = dp(i + 1, 0, k)
if i + m <= n: # If we have enough elements for a new segment
res = max(res,
dp(i + m, 1, k - 1) + (prefix[i + m] - prefix[i]))
return res
return dp(0, 0, k)
// Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
// class Solution {
// public int maxSum(int[] nums, int k, int m) {
// final int n = nums.length;
// int[] prefix = new int[n + 1];
// Integer[][][] mem = new Integer[n][2][k + 1];
// for (int i = 0; i < n; i++)
// prefix[i + 1] = prefix[i] + nums[i];
// return maxSum(nums, 0, 0, k, m, prefix, mem);
// }
//
// private static final int INF = 20_000_000;
//
// private int maxSum(int[] nums, int i, int ongoing, int k, int m, int[] prefix,
// Integer[][][] mem) {
// if (k < 0)
// return -INF;
// if (i == nums.length)
// return k == 0 ? 0 : -INF;
// if (mem[i][ongoing][k] != null)
// return mem[i][ongoing][k];
// if (ongoing == 1)
// // 1. End the current subarray (transition to state 0, same index i)
// // 2. Extend the current subarray by picking nums[i] and move to i + 1.
// return mem[i][1][k] = Math.max(maxSum(nums, i, 0, k, m, prefix, mem),
// maxSum(nums, i + 1, 1, k, m, prefix, mem) + nums[i]);
// // ongoing == 0
// // 1. Skip nums[i].
// // 2. Pick nums[i..i + m - 1] (only if k > 0 and there're enough elements).
// int res = maxSum(nums, i + 1, 0, k, m, prefix, mem);
// if (i + m <= nums.length) // If we have enough elements for a new segment
// res = Math.max(res,
// maxSum(nums, i + m, 1, k - 1, m, prefix, mem) + (prefix[i + m] - prefix[i]));
//
// return mem[i][0][k] = res;
// }
// }
// Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3473: Sum of K Subarrays With Length at Least M
// class Solution {
// public int maxSum(int[] nums, int k, int m) {
// final int n = nums.length;
// int[] prefix = new int[n + 1];
// Integer[][][] mem = new Integer[n][2][k + 1];
// for (int i = 0; i < n; i++)
// prefix[i + 1] = prefix[i] + nums[i];
// return maxSum(nums, 0, 0, k, m, prefix, mem);
// }
//
// private static final int INF = 20_000_000;
//
// private int maxSum(int[] nums, int i, int ongoing, int k, int m, int[] prefix,
// Integer[][][] mem) {
// if (k < 0)
// return -INF;
// if (i == nums.length)
// return k == 0 ? 0 : -INF;
// if (mem[i][ongoing][k] != null)
// return mem[i][ongoing][k];
// if (ongoing == 1)
// // 1. End the current subarray (transition to state 0, same index i)
// // 2. Extend the current subarray by picking nums[i] and move to i + 1.
// return mem[i][1][k] = Math.max(maxSum(nums, i, 0, k, m, prefix, mem),
// maxSum(nums, i + 1, 1, k, m, prefix, mem) + nums[i]);
// // ongoing == 0
// // 1. Skip nums[i].
// // 2. Pick nums[i..i + m - 1] (only if k > 0 and there're enough elements).
// int res = maxSum(nums, i + 1, 0, k, m, prefix, mem);
// if (i + m <= nums.length) // If we have enough elements for a new segment
// res = Math.max(res,
// maxSum(nums, i + m, 1, k - 1, m, prefix, mem) + (prefix[i + m] - prefix[i]));
//
// return mem[i][0][k] = res;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.