Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78ae1e2

Browse files
Create 3639-minimum-time-to-activate-string.js
1 parent 323c8e8 commit 78ae1e2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎3639-minimum-time-to-activate-string.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 n = s.length
9+
10+
function f(mid) {
11+
const st = s.split('')
12+
for (let i = 0; i < mid; i++) {
13+
st[order[i]] = '*'
14+
}
15+
16+
let total = 0
17+
let count = 0
18+
for (const ch of st) {
19+
if (ch === '*') {
20+
total += (count * (count + 1)) / 2
21+
count = 0
22+
} else {
23+
count += 1
24+
}
25+
}
26+
if (count > 0) {
27+
total += (count * (count + 1)) / 2
28+
}
29+
30+
const invalid = total
31+
const all_substrings = (n * (n + 1)) / 2
32+
const valid = all_substrings - invalid
33+
34+
return valid >= k
35+
}
36+
37+
let low = 0,
38+
high = n
39+
let res = -1
40+
while (low <= high) {
41+
const mid = (low + high) >> 1
42+
43+
if (f(mid)) {
44+
res = mid
45+
high = mid - 1
46+
} else {
47+
low = mid + 1
48+
}
49+
}
50+
51+
return res !== -1 ? res - 1 : res
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /