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: 8
💡 Note: The optimal division is [1] | [3,2] | [6,4,2]. The costs are 1 + 3 + 6 = 10. But we can also do [1] | [2] | [6,4,2] with costs 1 + 2 + 6 = 9. Actually the optimal is [1] | [2] | [4,2] with costs 1 + 2 + 4 = 7. Wait, let me recalculate: [1] | [3,2,6] | [4,2] gives 1 + 3 + 4 = 8.
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
Optimal Array Division StrategyStep 1: Fixed first subarraynums[0]Always starts hereStep 2: Choose k-1 splits within distance32642Split 1Split 2Distance ≤ distHeap-based OptimizationSmall Heap(k-1 minimum)Max HeapLarge Heap(remaining)Min HeapMaintain balance as window slidesTime: O(n log k) | Space: O(n + k)🎯 Key Insight: Use DP + sliding window with heaps for efficient k-1 minimum tracking
Understanding the Visualization
1
Identify constraints
First subarray starts at 0, need exactly k parts, distance limit between 2nd and k-th
2
Use sliding window
For each possible starting position, maintain k-1 cheapest options within distance
3
Apply dynamic programming
Build solution from right to left, using optimal substructure
Key Takeaway
🎯 Key Insight: The optimal solution combines dynamic programming with a sliding window approach, using heaps to efficiently maintain the k-1 smallest values within the distance constraint as we process positions from right to left.
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