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 an integer array nums and an integer k.
Split the array into some number of non-empty subarrays. The cost of a split is the sum of the importance value of each subarray in the split.
Let trimmed(subarray) be the version of the subarray where all numbers which appear only once are removed.
trimmed([3,1,2,4,3,4]) = [3,4,3,4].The importance value of a subarray is k + trimmed(subarray).length.
[1,2,3,3,3,4,4], then trimmed([1,2,3,3,3,4,4]) = [3,3,3,4,4].The importance value of this subarray will be k + 5.Return the minimum possible cost of a split of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1,2,1,3,3], k = 2 Output: 8 Explanation: We split nums to have two subarrays: [1,2], [1,2,1,3,3]. The importance value of [1,2] is 2 + (0) = 2. The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6. The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.
Example 2:
Input: nums = [1,2,1,2,1], k = 2 Output: 6 Explanation: We split nums to have two subarrays: [1,2], [1,2,1]. The importance value of [1,2] is 2 + (0) = 2. The importance value of [1,2,1] is 2 + (2) = 4. The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.
Example 3:
Input: nums = [1,2,1,2,1], k = 5 Output: 10 Explanation: We split nums to have one subarray: [1,2,1,2,1]. The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10. The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.
Constraints:
1 <= nums.length <= 10000 <= nums[i] < nums.length1 <= k <= 109Problem summary: You are given an integer array nums and an integer k. Split the array into some number of non-empty subarrays. The cost of a split is the sum of the importance value of each subarray in the split. Let trimmed(subarray) be the version of the subarray where all numbers which appear only once are removed. For example, trimmed([3,1,2,4,3,4]) = [3,4,3,4]. The importance value of a subarray is k + trimmed(subarray).length. For example, if a subarray is [1,2,3,3,3,4,4], then trimmed([1,2,3,3,3,4,4]) = [3,3,3,4,4].The importance value of this subarray will be k + 5. Return the minimum possible cost of a split of nums. A subarray is a contiguous non-empty sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Dynamic Programming
[1,2,1,2,1,3,3] 2
[1,2,1,2,1] 2
[1,2,1,2,1] 5
coin-change)split-array-largest-sum)divide-an-array-into-subarrays-with-minimum-cost-ii)minimum-sum-of-values-by-dividing-array)minimum-cost-to-divide-array-into-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2547: Minimum Cost to Split an Array
class Solution {
private Integer[] f;
private int[] nums;
private int n, k;
public int minCost(int[] nums, int k) {
n = nums.length;
this.k = k;
this.nums = nums;
f = new Integer[n];
return dfs(0);
}
private int dfs(int i) {
if (i >= n) {
return 0;
}
if (f[i] != null) {
return f[i];
}
int[] cnt = new int[n];
int one = 0;
int ans = 1 << 30;
for (int j = i; j < n; ++j) {
int x = ++cnt[nums[j]];
if (x == 1) {
++one;
} else if (x == 2) {
--one;
}
ans = Math.min(ans, k + j - i + 1 - one + dfs(j + 1));
}
return f[i] = ans;
}
}
// Accepted solution for LeetCode #2547: Minimum Cost to Split an Array
func minCost(nums []int, k int) int {
n := len(nums)
f := make([]int, n)
var dfs func(int) int
dfs = func(i int) int {
if i >= n {
return 0
}
if f[i] > 0 {
return f[i]
}
ans, one := 1<<30, 0
cnt := make([]int, n)
for j := i; j < n; j++ {
cnt[nums[j]]++
x := cnt[nums[j]]
if x == 1 {
one++
} else if x == 2 {
one--
}
ans = min(ans, k+j-i+1-one+dfs(j+1))
}
f[i] = ans
return ans
}
return dfs(0)
}
# Accepted solution for LeetCode #2547: Minimum Cost to Split an Array
class Solution:
def minCost(self, nums: List[int], k: int) -> int:
@cache
def dfs(i):
if i >= n:
return 0
cnt = Counter()
one = 0
ans = inf
for j in range(i, n):
cnt[nums[j]] += 1
if cnt[nums[j]] == 1:
one += 1
elif cnt[nums[j]] == 2:
one -= 1
ans = min(ans, k + j - i + 1 - one + dfs(j + 1))
return ans
n = len(nums)
return dfs(0)
// Accepted solution for LeetCode #2547: Minimum Cost to Split an Array
// 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 #2547: Minimum Cost to Split an Array
// class Solution {
// private Integer[] f;
// private int[] nums;
// private int n, k;
//
// public int minCost(int[] nums, int k) {
// n = nums.length;
// this.k = k;
// this.nums = nums;
// f = new Integer[n];
// return dfs(0);
// }
//
// private int dfs(int i) {
// if (i >= n) {
// return 0;
// }
// if (f[i] != null) {
// return f[i];
// }
// int[] cnt = new int[n];
// int one = 0;
// int ans = 1 << 30;
// for (int j = i; j < n; ++j) {
// int x = ++cnt[nums[j]];
// if (x == 1) {
// ++one;
// } else if (x == 2) {
// --one;
// }
// ans = Math.min(ans, k + j - i + 1 - one + dfs(j + 1));
// }
// return f[i] = ans;
// }
// }
// Accepted solution for LeetCode #2547: Minimum Cost to Split an Array
function minCost(nums: number[], k: number): number {
const n = nums.length;
const f = new Array(n).fill(0);
const dfs = (i: number) => {
if (i >= n) {
return 0;
}
if (f[i]) {
return f[i];
}
const cnt = new Array(n).fill(0);
let one = 0;
let ans = 1 << 30;
for (let j = i; j < n; ++j) {
const x = ++cnt[nums[j]];
if (x == 1) {
++one;
} else if (x == 2) {
--one;
}
ans = Math.min(ans, k + j - i + 1 - one + dfs(j + 1));
}
f[i] = ans;
return f[i];
};
return dfs(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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
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.