-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
earliest-second-to-mark-indices-i.cpp
40 lines (38 loc) · 1.21 KB
/
earliest-second-to-mark-indices-i.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Time: O(mlogm)
// Space: O(n)
// binary search, greedy
class Solution {
public:
int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {
const auto& check = [&](int t) {
vector<int> lookup(size(nums), -1);
for (int i = 0; i < t; ++i) {
lookup[changeIndices[i] - 1] = i;
}
if (count(cbegin(lookup), cend(lookup), -1)) {
return false;
}
for (int i = 0, cnt = 0; i < t; ++i) {
if (i != lookup[changeIndices[i] - 1]) {
++cnt;
continue;
}
cnt -= nums[changeIndices[i] - 1];
if (cnt < 0) {
return false;
}
}
return true;
};
int64_t left = accumulate(cbegin(nums), cend(nums), 0ll) + size(nums), right = size(changeIndices);
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left <= size(changeIndices) ? left : -1;
}
};