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 decrementnums[i] by 1.
If nums[changeIndices[s]] is equal to 0, mark the index changeIndices[s].
Do nothing.
Return _an integer denoting theearliest second in the range _[1, m]_whenall indices in _numscan be marked by choosing operations optimally, or-1if it is impossible.
Input: nums =[2,2,0], changeIndices =[2,2,2,2,3,2,2,1]Output: 8Explanation: In this example, we have 8 seconds. The following operations can be performed to mark all indices:Second 1: Choose index 1 and decrement nums[1] by one. nums becomes [1,2,0].Second 2: Choose index 1 and decrement nums[1] by one. nums becomes [0,2,0].Second 3: Choose index 2 and decrement nums[2] by one. nums becomes [0,1,0].Second 4: Choose index 2 and decrement nums[2] by one. nums becomes [0,0,0].Second 5: Mark the index changeIndices[5], which is marking index 3, since nums[3]is equal to 0.Second 6: Mark the index changeIndices[6], which is marking index 2, since nums[2]is equal to 0.Second 7: Do nothing.Second 8: Mark the index changeIndices[8], which is marking index 1, since nums[1]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 8th second.Hence, the answer is8.
Input: nums =[1,3], changeIndices =[1,1,1,2,1,1,1]Output: 6Explanation: In this example, we have 7 seconds. The following operations can be performed to mark all indices:Second 1: Choose index 2 and decrement nums[2] by one. nums becomes [1,2].Second 2: Choose index 2 and decrement nums[2] by one. nums becomes [1,1].Second 3: Choose index 2 and decrement nums[2] by one. nums becomes [1,0].Second 4: Mark the index changeIndices[4], which is marking index 2, since nums[2]is equal to 0.Second 5: Choose index 1 and decrement nums[1] by one. nums becomes [0,0].Second 6: Mark the index changeIndices[6], which is marking index 1, since nums[1]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 is6.
Input: nums =[0,1], changeIndices =[2,2,2]Output: -1Explanation: In this example, it is impossible to mark all indices because index 1 isn't in changeIndices.Hence, the answer is-1.
We want to mark all indices in nums as soon as possible. Since we can only mark an index when its value is zero at the second specified by changeIndices, we need to optimally decrement values so that all indices reach zero at the right time. We simulate the process, always decrementing the largest nonzero values first, and try all possible seconds to see if it’s possible to mark all indices by that time.
classSolution {
public:int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {
int n = nums.size(), m = changeIndices.size();
auto check = [&](int t) ->bool {
vector<int> last(n, -1);
for (int i =0; i < t; ++i) last[changeIndices[i] -1] = i;
for (int i =0; i < n; ++i) if (last[i] ==-1) return false;
vector<int> ops;
for (int i =0; i < n; ++i) ops.push_back(last[i] - nums[i]);
sort(ops.begin(), ops.end());
for (int i =0; i < n; ++i) if (ops[i] < i) return false;
return true;
};
int l = n, r = m, ans =-1;
while (l <= r) {
int mid = (l + r) /2;
if (check(mid)) ans = mid, r = mid -1;
else l = mid +1;
}
return ans;
}
};
classSolution {
publicintearliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
int n = nums.length, m = changeIndices.length;
int l = n, r = m, ans =-1;
while (l <= r) {
int mid = (l + r) / 2;
if (check(nums, changeIndices, mid)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
privatebooleancheck(int[] nums, int[] changeIndices, int t) {
int n = nums.length;
int[] last =newint[n];
Arrays.fill(last, -1);
for (int i = 0; i < t; ++i) last[changeIndices[i]- 1]= i;
for (int i = 0; i < n; ++i) if (last[i]==-1) returnfalse;
int[] ops =newint[n];
for (int i = 0; i < n; ++i) ops[i]= last[i]- nums[i];
Arrays.sort(ops);
for (int i = 0; i < n; ++i) if (ops[i]< i) returnfalse;
returntrue;
}
}
classSolution {
funearliestSecondToMarkIndices(nums: IntArray, changeIndices: IntArray): Int {
val n = nums.size
val m = changeIndices.size
funcheck(t: Int): Boolean {
val last = IntArray(n) { -1 }
for (i in0 until t) last[changeIndices[i] - 1] = i
if (last.any { it== -1 }) returnfalseval ops = IntArray(n) { last[it] - nums[it] }.sorted()
for (i in0 until n) if (ops[i] < i) returnfalsereturntrue }
var l = n
var r = m
var ans = -1while (l <= r) {
val mid = (l + r) / 2if (check(mid)) {
ans = mid
r = mid - 1 } else {
l = mid + 1 }
}
return ans
}
}
classSolution:
defearliestSecondToMarkIndices(self, nums: list[int], changeIndices: list[int]) -> int:
n, m = len(nums), len(changeIndices)
defcheck(t: int) -> bool:
last = [-1] * n
for i in range(t):
last[changeIndices[i] -1] = i
if-1in last:
returnFalse ops = sorted(last[i] - nums[i] for i in range(n))
for i in range(n):
if ops[i] < i:
returnFalsereturnTrue l, r, ans = n, m, -1while l <= r:
mid = (l + r) //2if check(mid):
ans = mid
r = mid -1else:
l = mid +1return ans
impl Solution {
pubfnearliest_second_to_mark_indices(nums: Vec<i32>, change_indices: Vec<i32>) -> i32 {
let n = nums.len();
let m = change_indices.len();
letmut l = n;
letmut r = m;
letmut ans =-1;
while l <= r {
let mid = (l + r) /2;
if check(&nums, &change_indices, mid) {
ans = mid asi32;
r = mid -1;
} else {
l = mid +1;
}
}
ans asi32 }
}
fncheck(nums: &Vec<i32>, change_indices: &Vec<i32>, t: usize) -> bool {
let n = nums.len();
letmut last =vec![-1; n];
for i in0..t {
last[change_indices[i] asusize-1] = i asi32;
}
if last.iter().any(|&x| x ==-1) {
returnfalse;
}
letmut ops: Vec<i32>= last.iter().enumerate().map(|(i, &v)| v - nums[i]).collect();
ops.sort();
for (i, &v) in ops.iter().enumerate() {
if v < i asi32 {
returnfalse;
}
}
true}
⏰ Time complexity: O(m log m + n log n), where m is the length of changeIndices and n is the length of nums. Each check is O(n log n), and binary search is O(log m).