Divide an Array Into Subarrays With Minimum Cost II - Problem

You are given a 0-indexed integer array nums of length n, and two positive integers k and dist.

The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1, while the cost of [3,4,1] is 3.

You need to divide nums into exactly k disjoint contiguous subarrays, such that the difference between the starting index of the second subarray and the starting index of the k-th subarray should be at most dist.

In other words, if you divide nums into subarrays nums[0..(i₁-1)], nums[i₁..(i₂-1)], ..., nums[iₖ₋₁..(n-1)], then iₖ₋₁ - i₁ ≤ dist.

Return the minimum possible sum of the costs of these subarrays.

Example: If nums = [1,3,2,6,4,2], k = 3, and dist = 3, you might divide it as [1] + [3,2] + [6,4,2] with costs 1 + 3 + 6 = 10.

Input & Output

example_1.py — Basic case
$ Input: nums = [1,3,2,6,4,2], k = 3, dist = 3
Output: 5
💡 Note: The optimal division is [1,3] | [2,6,4] | [2]. The starting indices are 0, 2, 5. Distance constraint: 5-2=3 ≤ 3 ✓. Costs are 1 + 2 + 2 = 5.
example_2.py — Minimum k
$ Input: nums = [10,1,2,2,2,1], k = 2, dist = 4
Output: 11
💡 Note: We need exactly 2 subarrays. The distance between the 2nd subarray start and 2nd subarray start is 0 ≤ 4. Optimal division: [10] | [1,2,2,2,1] with costs 10 + 1 = 11.
example_3.py — Distance constraint
$ Input: nums = [1,2,3,4], k = 3, dist = 1
Output: 6
💡 Note: With dist = 1, the 2nd and 3rd subarrays must start within 1 position of each other. Division: [1] | [2] | [3,4] gives costs 1 + 2 + 3 = 6.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 2 ≤ k ≤ n
  • 1 ≤ dist ≤ n - 2
  • The first subarray always starts at index 0

Visualization

Tap to expand
Divide Array Into Subarrays With Min Cost II INPUT nums array: 1 i=0 3 i=1 2 i=2 6 i=3 4 i=4 2 i=5 k = 3 dist = 3 Need k=3 subarrays Window constraint: i(k-1) - i1 <= 3 Valid window: indices 1-4 Pick k-1=2 split points here Cost = sum of first elements of each subarray Minimize total cost! ALGORITHM STEPS 1 First element fixed nums[0]=1 always in cost 2 Sliding window Window size = dist+1 = 4 3 Two heaps/sets Track k-1 smallest in window 4 Slide and update Track minimum sum Data Structure: Two Multisets Selected (k-1) {2, 3} sum = 5 Candidates {4, 6} remaining Window [1,2,3,4]: values [3,2,6,4] Pick 2 smallest: 2, 3 Cost = 1 + 2 + 3 = 6? No! Best found: 1 + 5 + 2 = 8 FINAL RESULT Optimal Partition: [1] cost=1 [3,2,6] cost=3 [4,2] cost=4 Better partition found: [1] cost=1 [3,2,6,4] cost=3 [2] cost=4 Optimal: [1], [3,2], [6,4,2] Total Cost Calculation: 1 + 3 + 4 = 8 Output: 8 [OK] i1=1, i2=3: 3-1=2 <= 3 Key Insight: Use two balanced BSTs (multisets) to maintain the k-1 smallest elements in a sliding window of size dist+1. As the window slides, efficiently add/remove elements and transfer between sets to keep the k-1 smallest sum updated. Time: O(n log n) | Space: O(n) for the two ordered sets TutorialsPoint - Divide an Array Into Subarrays With Minimum Cost II | Optimal Solution
Asked in
Google 42 Meta 35 Amazon 28 Microsoft 22
62.0K Views
Medium Frequency
~35 min Avg. Time
1.9K 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