|
| 1 | +# [Problem 3013: Divide an Array Into Subarrays With Minimum Cost II](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I can’t share raw stream-of-consciousness internal chain-of-thought, but here is a concise summary of the key observation and approach: |
| 5 | +- The cost equals nums[0] plus the sum of the starts of the remaining k-1 subarrays. |
| 6 | +- The indices of those k-1 starts must all lie in a contiguous index interval whose length is at most dist (because max(start) - min(start) <= dist). So they must be chosen inside some window of indices of size at most dist+1. |
| 7 | +- Therefore the problem reduces to: find a window of indices among positions 1..n-1 such that the sum of the smallest (k-1) numbers inside that window is minimized. The answer is nums[0] plus that minimum sum. |
| 8 | + |
| 9 | +This leads to a sliding-window + data-structure solution that maintains the sum of the smallest (k-1) elements in the current window as the window slides. |
| 10 | + |
| 11 | +## Refining the problem, round 2 thoughts |
| 12 | +I can’t provide additional internal chain-of-thought, but here is a concise refinement of the plan and edge considerations: |
| 13 | +- We will iterate all windows [L, R] with L in [1..n-1] and R = min(n-1, L + dist). Each window length is at most dist+1. |
| 14 | +- For each window where window_size >= (k-1), we compute the sum of the smallest (k-1) numbers in that window and track the minimum across windows. |
| 15 | +- To maintain the sum of the smallest (k-1) elements while adding/removing single elements as L and R move by 1, use two heaps: |
| 16 | + - small (max-heap, implemented as negatives) contains the current smallest t elements (t = min(k-1, current_window_size)). |
| 17 | + - large (min-heap) contains the rest. |
| 18 | + - Maintain small_sum (sum of elements in small), and lazy deletion dicts to support removals when elements slide out. |
| 19 | +- Complexity: O(n log n) time (each element added/removed at most once, each heap op log n), O(n) extra space. |
| 20 | + |
| 21 | +## Attempted solution(s) |
| 22 | +```python |
| 23 | +from collections import defaultdict |
| 24 | +import heapq |
| 25 | + |
| 26 | +class SmallestKSliding: |
| 27 | + """ |
| 28 | + Maintain multiset of current window values and be able to |
| 29 | + report sum of smallest `target_k` elements (or sum of all if window smaller). |
| 30 | + Uses two heaps + lazy deletions: |
| 31 | + - small: max-heap (store negatives) containing the smallest elements |
| 32 | + - large: min-heap containing the rest |
| 33 | + """ |
| 34 | + def __init__(self): |
| 35 | + self.small = [] # max-heap via negatives |
| 36 | + self.large = [] # min-heap |
| 37 | + self.del_small = defaultdict(int) |
| 38 | + self.del_large = defaultdict(int) |
| 39 | + self.small_size = 0 |
| 40 | + self.large_size = 0 |
| 41 | + self.small_sum = 0 |
| 42 | + |
| 43 | + def _prune_small(self): |
| 44 | + # remove top elements from small that are marked deleted |
| 45 | + while self.small and self.del_small[-self.small[0]] > 0: |
| 46 | + val = -heapq.heappop(self.small) |
| 47 | + self.del_small[val] -= 1 |
| 48 | + |
| 49 | + def _prune_large(self): |
| 50 | + while self.large and self.del_large[self.large[0]] > 0: |
| 51 | + val = heapq.heappop(self.large) |
| 52 | + self.del_large[val] -= 1 |
| 53 | + |
| 54 | + def add(self, x): |
| 55 | + # insert x into appropriate heap |
| 56 | + if self.small and x > -self.small[0]: |
| 57 | + heapq.heappush(self.large, x) |
| 58 | + self.large_size += 1 |
| 59 | + else: |
| 60 | + heapq.heappush(self.small, -x) |
| 61 | + self.small_size += 1 |
| 62 | + self.small_sum += x |
| 63 | + |
| 64 | + def remove(self, x): |
| 65 | + # lazily remove x from relevant heap (compare to current boundary) |
| 66 | + # ensure top of small is pruned so comparison is correct |
| 67 | + self._prune_small() |
| 68 | + if self.small and x <= -self.small[0]: |
| 69 | + # belongs to small |
| 70 | + self.small_size -= 1 |
| 71 | + self.small_sum -= x |
| 72 | + self.del_small[x] += 1 |
| 73 | + # physically pop if it's at top |
| 74 | + if self.small and -self.small[0] == x: |
| 75 | + self._prune_small() |
| 76 | + else: |
| 77 | + # belongs to large |
| 78 | + self.large_size -= 1 |
| 79 | + self.del_large[x] += 1 |
| 80 | + if self.large and self.large[0] == x: |
| 81 | + self._prune_large() |
| 82 | + |
| 83 | + def rebalance(self, target_small_size): |
| 84 | + # Ensure small_size == target_small_size (or as large as possible if not enough elements) |
| 85 | + # First, clean tops |
| 86 | + self._prune_small() |
| 87 | + self._prune_large() |
| 88 | + |
| 89 | + # Move from small -> large if small too big |
| 90 | + while self.small_size > target_small_size: |
| 91 | + self._prune_small() |
| 92 | + if not self.small: |
| 93 | + break |
| 94 | + val = -heapq.heappop(self.small) |
| 95 | + self.small_size -= 1 |
| 96 | + self.small_sum -= val |
| 97 | + heapq.heappush(self.large, val) |
| 98 | + self.large_size += 1 |
| 99 | + self._prune_small() |
| 100 | + self._prune_large() |
| 101 | + |
| 102 | + # Move from large -> small if small too small |
| 103 | + while self.small_size < target_small_size and self.large_size > 0: |
| 104 | + self._prune_large() |
| 105 | + if not self.large: |
| 106 | + break |
| 107 | + val = heapq.heappop(self.large) |
| 108 | + self.large_size -= 1 |
| 109 | + heapq.heappush(self.small, -val) |
| 110 | + self.small_size += 1 |
| 111 | + self.small_sum += val |
| 112 | + self._prune_large() |
| 113 | + self._prune_small() |
| 114 | + |
| 115 | + def sum_small(self): |
| 116 | + # top pruning to keep structure consistent |
| 117 | + self._prune_small() |
| 118 | + return self.small_sum |
| 119 | + |
| 120 | + |
| 121 | +class Solution: |
| 122 | + def divideArray(self, nums, k, dist): |
| 123 | + n = len(nums) |
| 124 | + # number of starts to choose besides nums[0] |
| 125 | + m = k - 1 |
| 126 | + # if m == 0 would return nums[0], but k >= 3 per constraints |
| 127 | + if n == 0: |
| 128 | + return 0 |
| 129 | + |
| 130 | + sk = SmallestKSliding() |
| 131 | + ans = float('inf') |
| 132 | + |
| 133 | + # sliding window over indices [L .. R] within 1..n-1 |
| 134 | + R = 0 # current right index included in structure; 0 means none initially for indices >=1 |
| 135 | + # We'll iterate L from 1 to n-1 inclusive |
| 136 | + for L in range(1, n): |
| 137 | + # extend R to min(n-1, L + dist) |
| 138 | + newR = min(n - 1, L + dist) |
| 139 | + while R < newR: |
| 140 | + R += 1 |
| 141 | + sk.add(nums[R]) |
| 142 | + |
| 143 | + window_size = R - L + 1 |
| 144 | + if window_size >= m: |
| 145 | + # Ensure small contains exactly m smallest elements |
| 146 | + sk.rebalance(m) |
| 147 | + cur_sum_small = sk.sum_small() |
| 148 | + ans = min(ans, nums[0] + cur_sum_small) |
| 149 | + else: |
| 150 | + # maintain structure but small should be window size |
| 151 | + if window_size > 0: |
| 152 | + sk.rebalance(window_size) |
| 153 | + |
| 154 | + # before incrementing L in next iteration, remove nums[L] |
| 155 | + # (if L == R+1, the window is empty next iteration; remove handles gracefully) |
| 156 | + if window_size > 0: |
| 157 | + sk.remove(nums[L]) |
| 158 | + |
| 159 | + return ans if ans != float('inf') else -1 # problem guarantees feasible input |
| 160 | + |
| 161 | +# Example usage to match LeetCode signature |
| 162 | +def divideArray(nums, k, dist): |
| 163 | + return Solution().divideArray(nums, k, dist) |
| 164 | + |
| 165 | +# If used on LeetCode, class Solution would expose divideArray as specified. |
| 166 | +``` |
| 167 | + |
| 168 | +- Notes about approach: |
| 169 | + - Key reduction: the chosen k-1 start indices (excluding index 0) must all lie inside some index interval of length at most dist, so we only need to consider windows of indices and pick k-1 smallest numbers inside each window. |
| 170 | + - We maintain the sum of the smallest (k-1) numbers inside a sliding window using two heaps (max-heap for the chosen smallest group, min-heap for the rest) and lazy deletions for efficient removals as the window slides. |
| 171 | + - Time complexity: O(n log n) — each element is added/removed at most once; heap ops are logarithmic. |
| 172 | + - Space complexity: O(n) for the heaps and lazy deletion maps. |
0 commit comments