3013. Divide an Array Into Subarrays With Minimum Cost II Hard
1/**
2 * [3013] Divide an Array Into Subarrays With Minimum Cost II
3 *
4 * You are given a 0-indexed array of integers nums of length n, and two positive integers k and dist.
5 * 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.
6 * You need to divide nums into k disjoint contiguous <span data-keyword="subarray-nonempty">subarrays</span>, 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.
7 * Return the minimum possible sum of the cost of these subarrays.
8 *
9 * <strong class="example">Example 1:
10 *
11 * Input: nums = [1,3,2,6,4,2], k = 3, dist = 3
12 * Output: 5
13 * Explanation: The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
14 * It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
15 *
16 * <strong class="example">Example 2:
17 *
18 * Input: nums = [10,1,2,2,2,1], k = 4, dist = 3
19 * Output: 15
20 * Explanation: The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
21 * The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist.
22 * It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
23 *
24 * <strong class="example">Example 3:
25 *
26 * Input: nums = [10,8,18,9], k = 3, dist = 1
27 * Output: 36
28 * Explanation: The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
29 * The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist.
30 * It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
31 *
32 *
33 * Constraints:
34 *
35 * 3 <= n <= 10^5
36 * 1 <= nums[i] <= 10^9
37 * 3 <= k <= n
38 * k - 2 <= dist <= n - 2
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii/
44// discuss: https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn minimum_cost(nums: Vec<i32>, k: i32, dist: i32) -> i64 {
50
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_3013() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.