Earliest Second to Mark Indices II Problem
Description
LeetCode Problem 3049.
You are given two 1-indexed integer arrays, nums and, changeIndices, having lengths n and m, respectively.
Initially, all indices in nums are unmarked. Your task is to mark all indices in nums.
In each second, s, in order from 1 to m (inclusive), you can perform one of the following operations:
- Choose an index
iin the range[1, n]and decrementnums[i]by1. - Set
nums[changeIndices[s]]to any non-negative value. - Choose an index
iin the range[1, n], wherenums[i]is equal to0, and mark indexi. - Do nothing.
Return an integer denoting the earliest second in the range [1, m] when all indices in nums can be marked by choosing operations optimally, or -1 if it is impossible.
Example 1:
Input: nums = [3,2,3], changeIndices = [1,3,2,2,2,2,3]
Output: 6
Explanation: In this example, we have 7 seconds. The following operations can be performed to mark all indices:
Second 1: Set nums[changeIndices[1]] to 0. nums becomes [0,2,3].
Second 2: Set nums[changeIndices[2]] to 0. nums becomes [0,2,0].
Second 3: Set nums[changeIndices[3]] to 0. nums becomes [0,0,0].
Second 4: Mark index 1, since nums[1] is equal to 0.
Second 5: Mark index 2, since nums[2] is equal to 0.
Second 6: Mark index 3, since nums[3] is equal to 0.
Now all indices have been marked.
It can be shown that it is not possible to mark all indices earlier than the 6th second.
Hence, the answer is 6.
Example 2:
Input: nums = [0,0,1,2], changeIndices = [1,2,1,2,1,2,1,2]
Output: 7
Explanation: In this example, we have 8 seconds. The following operations can be performed to mark all indices:
Second 1: Mark index 1, since nums[1] is equal to 0.
Second 2: Mark index 2, since nums[2] is equal to 0.
Second 3: Decrement index 4 by one. nums becomes [0,0,1,1].
Second 4: Decrement index 4 by one. nums becomes [0,0,1,0].
Second 5: Decrement index 3 by one. nums becomes [0,0,0,0].
Second 6: Mark index 3, since nums[3] is equal to 0.
Second 7: Mark index 4, since nums[4] is equal to 0.
Now all indices have been marked.
It can be shown that it is not possible to mark all indices earlier than the 7th second.
Hence, the answer is 7.
Example 3:
Input: nums = [1,2,3], changeIndices = [1,2,3]
Output: -1
Explanation: In this example, it can be shown that it is impossible to mark all indices, as we don't have enough seconds.
Hence, the answer is -1.
Constraints:
1 <= n == nums.length <= 50000 <= nums[i] <= 10^91 <= m == changeIndices.length <= 50001 <= changeIndices[i] <= n
Solution
Approach: Binary Search + Greedy with a Heap
If all indices can be marked within t seconds, they can also be marked within any larger t, so binary search the smallest feasible t (answer -1 if even m fails).
To check a given t, look at what each index needs. An index can be handled in two ways:
- By decrements:
nums[i]decrement seconds plus one mark second. - By a reset: spend the second
swherechangeIndices[s] = ito set it to0, then one later second to mark it. Only the first occurrence ofiwithin the firsttseconds is worth using. It costs 2 seconds, so it helps only whennums[i] > 1.
Scan the seconds from t down to 1, keeping free, the number of seconds not (yet) assigned to a reset or a reset's mark, and a min-heap of the values nums[i] of the indices that currently use a reset:
- A second that is not the first occurrence of a useful index is a free second:
free += 1. - At the first occurrence of an index with
nums[i] > 1: iffree > 0, use this second as a reset and one free second later in time to mark it (free -= 1, pushnums[i]). Otherwise, if the heap's smallest value is smaller thannums[i], swap: that cheaper index goes back to decrements (freeing its reset second and its mark second) and the current one takes the reset. If neither applies this second is just free.
At the end, the indices without a reset need sum(nums of those) + (their count) free seconds in total (decrements plus marks). The check passes when that is at most free. Each check costs O(m log m).
Complexity: time O(m log^2 m), space O(n + m).
import heapq
class Solution:
def earliestSecondToMarkIndices(self, nums: List[int], changeIndices: List[int]) -> int:
n, m = len(nums), len(changeIndices)
total = sum(nums)
def check(t: int) -> bool:
first = [-1] * n
for s in range(t):
i = changeIndices[s] - 1
if first[i] < 0:
first[i] = s
heap = []
saved = 0
free = 0
for s in range(t - 1, -1, -1):
i = changeIndices[s] - 1
if s == first[i] and nums[i] > 1:
if free > 0:
free -= 1
elif heap and heap[0] < nums[i]:
saved -= heapq.heappop(heap)
free += 1
else:
free += 1
continue
heapq.heappush(heap, nums[i])
saved += nums[i]
else:
free += 1
return total - saved + n - len(heap) <= free
l, r = 1, m + 1
while l < r:
mid = (l + r) // 2
if check(mid):
r = mid
else:
l = mid + 1
return -1 if l > m else l