Skip to content

Practice Divide an Array Into Subarrays With Minimum Cost II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3013
LeetCode Problem

Divide an Array Into Subarrays With Minimum Cost II

Problem Statement

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.

Source: Divide an Array Into Subarrays With Minimum Cost II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,3,2,6,4,2], k = 3, dist = 3
Output
5

# Example 2

Input
nums = [10,1,2,2,2,1], k = 4, dist = 3
Output
15

# Example 3

Input
nums = [10,8,18,9], k = 3, dist = 1
Output
36

Constraints

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

Related LeetCode Problems

Practice more problems similar to Divide an Array Into Subarrays With Minimum Cost II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.