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 5ce863c

Browse files
Create 3639.Minimum-Time-to-Activate-String.cpp
1 parent bb94cc3 commit 5ce863c

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+
using ll = long long;
2+
class Solution {
3+
public:
4+
int minTime(string s, vector<int>& order, int k) {
5+
int n = s.size();
6+
vector<bool>isStar(n, false);
7+
ll T = ll(n)*(n+1)/2;
8+
if (k>T) return -1;
9+
10+
auto check = [&](int mid) {
11+
fill(isStar.begin(), isStar.end(), false);
12+
for (int i=0; i<=mid; i++)
13+
isStar[order[i]] = true;
14+
15+
ll sumNon = 0;
16+
for (int i=0; i<n; ){
17+
if (isStar[i]) {
18+
i++;
19+
continue;
20+
}
21+
int j = i;
22+
while (j<n && !isStar[j])
23+
j++;
24+
ll len = j-i;
25+
sumNon += len*(len+1)/2;
26+
i = j;
27+
}
28+
return T - sumNon >= k;
29+
};
30+
31+
int lo = 0, hi = n-1, ans = -1;
32+
while (lo < hi) {
33+
int mid = lo + (hi-lo)/2;
34+
if (check(mid)) {
35+
hi = mid;
36+
} else {
37+
lo = mid+1;
38+
}
39+
}
40+
if (check(hi)) return hi;
41+
else return -1;
42+
}
43+
};

0 commit comments

Comments
(0)

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