Skip to content

Commit fb67ea7

Browse files
authored
Create 3696-minimum-time-to-activate-string.js
1 parent 6a0882f commit fb67ea7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {string} s
3+
* @param {number[]} order
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var minTime = function(s, order, k) {
8+
const len = order.length
9+
let l = 0, r = len
10+
const {floor: flr} = Math
11+
while(l < r) {
12+
const mid = flr((l + r) / 2)
13+
if(isOK(mid)) r = mid
14+
else l = mid + 1
15+
}
16+
17+
return l >= len ? -1 : l
18+
19+
function isOK(t) {
20+
const n = s.length
21+
22+
const isStar = Array(n).fill(false)
23+
let inValid = 0, segLen = 0
24+
25+
for(let i = 0; i <= t; i++) isStar[order[i]] = true
26+
27+
for(let i = 0; i < n; i++) {
28+
if(isStar[i]) {
29+
inValid += segLen * (segLen + 1) / 2
30+
segLen = 0
31+
} else {
32+
segLen++
33+
}
34+
}
35+
36+
if(segLen) inValid += segLen * (segLen + 1) / 2
37+
38+
const all = n * (n + 1) / 2
39+
40+
const valid = all - inValid
41+
return valid >= k
42+
}
43+
};

0 commit comments

Comments
 (0)