Skip to content

Commit c61eef2

Browse files
authored
Create 3048-earliest-second-to-mark-indices-i.js
1 parent 0fb6b09 commit c61eef2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[]} changeIndices
4+
* @return {number}
5+
*/
6+
var earliestSecondToMarkIndices = function(nums, changeIndices) {
7+
let isPossible = function (mid) {
8+
let last = new Uint16Array(nums.length);
9+
for (let i = 0; i <= mid; ++i) last[changeIndices[i]] = i;
10+
11+
let count = 0, marked = 0;
12+
for (let i = 0; i <= mid; ++i) {
13+
if (last[changeIndices[i]] === i) {
14+
count -= nums[changeIndices[i]];
15+
if (count < 0) return false;
16+
++marked;
17+
} else {
18+
++count;
19+
}
20+
}
21+
return marked === nums.length;
22+
};
23+
24+
changeIndices = changeIndices.map((x) => x - 1);
25+
let l = 0, r = changeIndices.length - 1;
26+
while (l < r) {
27+
let mid = (l + r) >> 1;
28+
isPossible(mid) ? r = mid : l = mid + 1;
29+
}
30+
return isPossible(l) ? l + 1 : -1;
31+
};

0 commit comments

Comments
 (0)