Minimum Cost to Split an Array - Problem

You are given an integer array nums and an integer k.

Your goal: Split the array into multiple non-empty subarrays to minimize the total cost of the split.

How cost is calculated:

  1. For each subarray, create a trimmed version by removing all numbers that appear only once
  2. The importance value of a subarray = k + length of trimmed subarray
  3. Total cost = sum of importance values of all subarrays

Example:
If subarray is [3,1,2,4,3,4]:
• Trimmed version: [3,4,3,4] (removed 1 and 2)
• Importance value: k + 4

Key insight: You want to balance between having fewer splits (each costs at least k) and having smaller trimmed lengths (elements that repeat within the subarray).

Input & Output

example_1.py — Basic Split Decision
$ Input: nums = [3,1,2,4,3,4], k = 3
› Output: 8
šŸ’” Note: Optimal split: [3,1] and [2,4,3,4]. First subarray has no duplicates, so cost = 3+0=3. Second subarray has duplicates [4,4], so trimmed=[4,4] with length 2, cost = 3+2=5. Total = 3+5=8.
example_2.py — Single Subarray
$ Input: nums = [1,2,1,2,1,3,3], k = 2
› Output: 8
šŸ’” Note: Keep as one subarray [1,2,1,2,1,3,3]. Duplicates are [1,1,1,2,2,3,3] (6 elements), so cost = 2+6=8. Splitting would cost more due to multiple k values.
example_3.py — All Unique Elements
$ Input: nums = [1,2,3,4,5], k = 2
› Output: 10
šŸ’” Note: Since all elements are unique, trimmed length is always 0. Best strategy is to split into individual elements: 5 subarrays each costing k=2, total = 5Ɨ2=10.

Visualization

Tap to expand
Warehouse Package Optimization: Items [3,1,2,4,3,4], k=3šŸ“¦ Option 1: Single BoxAll items: [3,1,2,4,3,4]Duplicates: 3,3,4,4 (4 items)Cost: 3 + 4 = 7šŸ“¦šŸ“¦ Option 2: Split BoxesBox 1: [3,1] + Box 2: [2,4,3,4]Duplicates: 0 + 2 (just 4,4)Cost: (3+0) + (3+2) = 8šŸŽÆ Decision: Choose Option 1Single box costs 7 < Split boxes cost 8Dynamic Programming tries all possible ways to split optimallyKey insight: Balance base costs (k per box) vs duplicate handling costsāœ“
Understanding the Visualization
1
Analyze Items
Look at your items: [3,1,2,4,3,4]. Items 3 and 4 appear twice.
2
Consider Box Options
Option 1: One big box costs k + 4 duplicates. Option 2: Split into smaller boxes.
3
Calculate Costs
Split [3,1] + [2,4,3,4]: First box k+0, second box k+2, total 2k+2.
4
Choose Optimal
Compare 1Ɨk+4 vs 2Ɨk+2. When k=3: 7 vs 8, so one box wins!
Key Takeaway
šŸŽÆ Key Insight: Dynamic programming finds the optimal balance between minimizing the number of boxes (each costing k) and minimizing duplicate handling fees within each box.

Time & Space Complexity

Time Complexity
ā±ļø
O(n³)

O(n²) pairs of split points, each requiring O(n) time to compute subarray cost

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

DP array of size n plus space for frequency counting

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ nums.length
  • 1 ≤ k ≤ 109
  • All subarrays must be non-empty
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 20
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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