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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given two integer arrays, nums and cost, of the same size, and an integer k.
You can divide nums into subarrays. The cost of the ith subarray consisting of elements nums[l..r] is:
(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r]).Note that i represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.
Return the minimum total cost possible from any valid division.
Example 1:
Input: nums = [3,1,4], cost = [4,6,6], k = 1
Output: 110
Explanation:
The minimum total cost possible can be achieved by dividingnums into subarrays [3, 1] and [4].
[3,1] is (3 + 1 + 1 * 1) * (4 + 6) = 50.[4] is (3 + 1 + 4 + 1 * 2) * 6 = 60.Example 2:
Input: nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
Output: 985
Explanation:
The minimum total cost possible can be achieved by dividingnums into subarrays [4, 8, 5, 1], [14, 2, 2], and [12, 1].
[4, 8, 5, 1] is (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525.[14, 2, 2] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250.[12, 1] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210.Constraints:
1 <= nums.length <= 1000cost.length == nums.length1 <= nums[i], cost[i] <= 10001 <= k <= 1000Problem summary: You are given two integer arrays, nums and cost, of the same size, and an integer k. You can divide nums into subarrays. The cost of the ith subarray consisting of elements nums[l..r] is: (nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r]). Note that i represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on. Return the minimum total cost possible from any valid division.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[3,1,4] [4,6,6] 1
[4,8,5,1,14,2,2,12,1] [7,2,8,4,2,2,1,1,2] 7
minimum-cost-to-split-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3500: Minimum Cost to Divide Array Into Subarrays
class Solution {
public long minimumCost(int[] nums, int[] cost, int k) {
final int n = nums.length;
long[] prefixNums = new long[n + 1];
long[] prefixCost = new long[n + 1];
// dp[i] := the minimum cost to divide nums[i..n - 1] into subarrays
long[] dp = new long[n + 1];
for (int i = 0; i < n; ++i) {
prefixNums[i + 1] = prefixNums[i] + nums[i];
prefixCost[i + 1] = prefixCost[i] + cost[i];
}
Arrays.fill(dp, Long.MAX_VALUE);
dp[n] = 0;
for (int i = n - 1; i >= 0; --i)
for (int j = i; j < n; ++j)
dp[i] = Math.min(dp[i], prefixNums[j + 1] * (prefixCost[j + 1] - prefixCost[i]) +
k * (prefixCost[n] - prefixCost[i]) + dp[j + 1]);
return dp[0];
}
}
// Accepted solution for LeetCode #3500: Minimum Cost to Divide Array Into Subarrays
package main
import (
"math"
)
// https://space.bilibili.com/206214
type vec struct{ x, y int }
func (a vec) sub(b vec) vec { return vec{a.x - b.x, a.y - b.y} }
func (a vec) det(b vec) int { return a.x*b.y - a.y*b.x }
func (a vec) dot(b vec) int { return a.x*b.x + a.y*b.y }
func minimumCost(nums, cost []int, k int) int64 {
totalCost := 0
for _, c := range cost {
totalCost += c
}
q := []vec{{}}
sumNum, sumCost := 0, 0
for i, x := range nums {
sumNum += x
sumCost += cost[i]
p := vec{-sumNum - k, 1}
for len(q) > 1 && p.dot(q[0]) >= p.dot(q[1]) {
q = q[1:]
}
// 一边算 DP 一边构建下凸包
p = vec{sumCost, p.dot(q[0]) + sumNum*sumCost + k*totalCost}
for len(q) > 1 && q[len(q)-1].sub(q[len(q)-2]).det(p.sub(q[len(q)-1])) <= 0 {
q = q[:len(q)-1]
}
q = append(q, p)
}
return int64(q[len(q)-1].y)
}
func minimumCost1(nums, cost []int, k int) int64 {
n := len(nums)
s := make([]int, n+1)
for i, c := range cost {
s[i+1] = s[i] + c
}
f := make([]int, n+1)
sumNum := 0
for i, x := range nums {
i++
sumNum += x
res := math.MaxInt
for j := range i {
res = min(res, f[j]+sumNum*(s[i]-s[j])+k*(s[n]-s[j]))
}
f[i] = res
}
return int64(f[n])
}
# Accepted solution for LeetCode #3500: Minimum Cost to Divide Array Into Subarrays
class Solution:
def minimumCost(self, nums: list[int], cost: list[int], k: int) -> int:
n = len(nums)
prefixNums = list(itertools.accumulate(nums, initial=0))
prefixCost = list(itertools.accumulate(cost, initial=0))
# dp[i] := the minimum cost to divide nums[i..n - 1] into subarrays
dp = [math.inf] * n + [0]
for i in range(n - 1, -1, -1):
for j in range(i, n):
dp[i] = min(dp[i],
prefixNums[j + 1] * (prefixCost[j + 1] - prefixCost[i]) +
k * (prefixCost[n] - prefixCost[i]) + dp[j + 1])
return dp[0]
// Accepted solution for LeetCode #3500: Minimum Cost to Divide Array Into Subarrays
// 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 #3500: Minimum Cost to Divide Array Into Subarrays
// class Solution {
// public long minimumCost(int[] nums, int[] cost, int k) {
// final int n = nums.length;
// long[] prefixNums = new long[n + 1];
// long[] prefixCost = new long[n + 1];
// // dp[i] := the minimum cost to divide nums[i..n - 1] into subarrays
// long[] dp = new long[n + 1];
//
// for (int i = 0; i < n; ++i) {
// prefixNums[i + 1] = prefixNums[i] + nums[i];
// prefixCost[i + 1] = prefixCost[i] + cost[i];
// }
//
// Arrays.fill(dp, Long.MAX_VALUE);
// dp[n] = 0;
//
// for (int i = n - 1; i >= 0; --i)
// for (int j = i; j < n; ++j)
// dp[i] = Math.min(dp[i], prefixNums[j + 1] * (prefixCost[j + 1] - prefixCost[i]) +
// k * (prefixCost[n] - prefixCost[i]) + dp[j + 1]);
//
// return dp[0];
// }
// }
// Accepted solution for LeetCode #3500: Minimum Cost to Divide Array Into Subarrays
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3500: Minimum Cost to Divide Array Into Subarrays
// class Solution {
// public long minimumCost(int[] nums, int[] cost, int k) {
// final int n = nums.length;
// long[] prefixNums = new long[n + 1];
// long[] prefixCost = new long[n + 1];
// // dp[i] := the minimum cost to divide nums[i..n - 1] into subarrays
// long[] dp = new long[n + 1];
//
// for (int i = 0; i < n; ++i) {
// prefixNums[i + 1] = prefixNums[i] + nums[i];
// prefixCost[i + 1] = prefixCost[i] + cost[i];
// }
//
// Arrays.fill(dp, Long.MAX_VALUE);
// dp[n] = 0;
//
// for (int i = n - 1; i >= 0; --i)
// for (int j = i; j < n; ++j)
// dp[i] = Math.min(dp[i], prefixNums[j + 1] * (prefixCost[j + 1] - prefixCost[i]) +
// k * (prefixCost[n] - prefixCost[i]) + dp[j + 1]);
//
// return dp[0];
// }
// }
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.