Divide an Array Into Subarrays With Minimum Cost II Problem
Description
LeetCode Problem 3013.
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..(i_1 - 1)], nums[i_1..(i_2 - 1)], ..., nums[i_(k-1)..(n - 1)], then i_(k-1) - i_1 <= dist.
Return the minimum possible sum of the cost of these subarrays.
Example 1:
Input: nums = [1,3,2,6,4,2], k = 3, dist = 3
Output: 5
Explanation: The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i_(k-1) - i_1 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.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
Example 2:
Input: nums = [10,1,2,2,2,1], k = 4, dist = 3
Output: 15
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i_(k-1) - i_1 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.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i_(k-1) and i_1 is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
Example 3:
Input: nums = [10,8,18,9], k = 3, dist = 1
Output: 36
Explanation: The best possible way to divide nums into 3 subarrays is: [10], [8], and [18,9]. This choice is valid because i_(k-1) - i_1 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.
The division [10], [8,18], and [9] is not valid, because the difference between i_(k-1) and i_1 is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
Constraints:
3 <= n <= 10^51 <= nums[i] <= 10^93 <= k <= nk - 2 <= dist <= n - 2
Solution
Approach: Sliding Window of the Smallest Values
The first subarray starts at nums[0]. The other k - 1 starts must all fit in a window of dist + 1 consecutive positions beginning at the second start. So the answer is nums[0] plus the smallest possible sum of k - 1 values taken from a window of dist + 1 elements, minimized over every window position starting at index 1. (Taking the k - 1 smallest in the window never breaks the distance rule, because they all lie in that window.)
Slide the window along the array: remove the element that leaves, add the one that enters, and ask for the sum of the k - 1 smallest values currently inside. This needs a sorted structure that supports insert, delete and "sum of the t smallest": two ordered sets (one with the k - 1 smallest, one with the rest, rebalanced after each step) or a Fenwick tree over the compressed values both work.
Complexity: O(n log n) time and O(n) space.
class Solution:
def minimumCost(self, nums: List[int], k: int, dist: int) -> int:
n = len(nums)
vals = sorted(set(nums))
m = len(vals)
rank = {v: i + 1 for i, v in enumerate(vals)}
cnt = [0] * (m + 1)
tot = [0] * (m + 1)
log = m.bit_length()
def update(v, d):
i = rank[v]
while i <= m:
cnt[i] += d
tot[i] += d * v
i += i & -i
def smallest(t):
# sum of the t smallest values in the window
pos = acc = s = 0
for p in range(log, -1, -1):
np_ = pos + (1 << p)
if np_ <= m and acc + cnt[np_] < t:
pos = np_
acc += cnt[np_]
s += tot[np_]
return s + (t - acc) * vals[pos]
for i in range(1, dist + 2):
update(nums[i], 1)
ans = smallest(k - 1)
for i in range(dist + 2, n):
update(nums[i - dist - 1], -1)
update(nums[i], 1)
ans = min(ans, smallest(k - 1))
return nums[0] + ans