Minimum Cost to Split an Array - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,2,2], k = 3
Output: 8
💡 Note: Split into [1,2,3,2] and [2]. First subarray has trimmed([1,2,3,2]) = [2,2] with length 2, so cost = 3+2 = 5. Second subarray has trimmed([2]) = [] with length 0, so cost = 3+0 = 3. Total = 5+3 = 8.
Example 2 — All Duplicates
$ Input: nums = [1,1,1,1], k = 2
Output: 6
💡 Note: Keep as single array [1,1,1,1]. trimmed([1,1,1,1]) = [1,1,1,1] with length 4. Cost = 2+4 = 6. Any split would cost more since each subarray adds k=2.
Example 3 — All Unique
$ Input: nums = [1,2,3,4], k = 1
Output: 4
💡 Note: Best to split into individual elements: [1], [2], [3], [4]. Each has trimmed length 0, so each costs 1+0 = 1. Total = 1+1+1+1 = 4.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000
  • 1 ≤ k ≤ 1000

Visualization

Tap to expand
Minimum Cost to Split an Array Dynamic Programming Approach INPUT nums array: 1 i=0 2 i=1 3 i=2 2 i=3 2 i=4 k = 3 Split array into subarrays Minimize total cost Highlighted cells (2,2,2) appear multiple times -- contribute to trimmed ALGORITHM STEPS 1 Define DP State dp[i] = min cost for nums[0..i] 2 Compute Importance importance = k + trimmed.length 3 Try All Splits For each j < i, try split at j 4 Take Minimum dp[i] = min(dp[j] + cost) Optimal Split Example: [1,2,3] trimmed=[] len=0 cost = 3+0 = 3 [2,2] trimmed=[2,2] len=2 cost = 3+2 = 5 Total = 3 + 5 = 8 FINAL RESULT Optimal Partition: [1,2,3] Cost: 3 [2,2] Cost: 5 Output: 8 OK - Minimum Cost Found Cost Breakdown: [1,2,3]: k=3, trim=0 --> 3 [2,2]: k=3, trim=2 --> 5 Total: 3 + 5 = 8 Key Insight: The trimmed length only counts elements appearing more than once. DP explores all possible split points, computing importance for each subarray. For [1,2,3], no duplicates so trimmed=0. For [2,2], both 2s are duplicates so trimmed length=2. This greedy grouping of duplicates minimizes total cost. TutorialsPoint - Minimum Cost to Split an Array | Dynamic Programming Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.4K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen