Divide an Array Into Subarrays With Minimum Cost II

You are given a 0-indexed array of integers 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 k disjoint contiguous subarrays, such that the difference between the starting index of the second subarray and the starting index of the kth subarray should be less than or equal to dist.

In other words, if you divide nums into the subarrays nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)], then ik-1 - i1 <= dist.

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

Example 1
Inputnums = [1,3,2,6,4,2], k = 3, dist = 3
Output5
The best valid division is [1,3], [2,6,4], and [2], giving total cost 1 + 2 + 2 = 5.
Example 2
Inputnums = [10,1,2,2,2,1], k = 4, dist = 3
Output15
The best valid division is [10], [1], [2], and [2,2,1], giving total cost 10 + 1 + 2 + 2 = 15.

Constraints

  • 3 <= n <= 10^5
  • 1 <= nums[i] <= 10^9
  • 3 <= k <= n
  • k - 2 <= dist <= n - 2

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate