Earliest Second to Mark Indices II
hard · failed · 22.4% accepted · 83 likes · top 2%
array · binary search · greedy · heap (priority queue)
Description
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 i in the range [1, n] and decrement nums[i] by 1.
- Set nums[changeIndices[s]] to any non-negative value.
- Choose an index i in the range [1, n], where nums[i] is equal to 0, and mark index i.
- 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:
Example 2:
Example 3:
Solution