Skip to content

Commit 5386309

Browse files
committed
Runtime: 1433 ms (Top 23.41%) | Memory: 179.2 MB (Top 26.07%)
1 parent c49494e commit 5386309

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
// Runtime: 1433 ms (Top 23.41%) | Memory: 179.2 MB (Top 26.07%)
12
class Solution {
23
public:
34
vector<int> longestRepeating(string s, string queryCharacters, vector<int>& queryIndices) {
45
multiset<int> ls; // ordered set of lengths of all intervals at present
56
set<int> ins; // ordered set representing the left-ends of intervals
67
int n = s.length();
78
ins.insert(n); // allow us to get length of current interval by *next(it) - *it for all cases
8-
9+
910
// initialize the ordered sets with the original string
1011
for (int i = 0, j = 1; i < n;) {
1112
while (j < n && s[j] == s[j-1]) ++j;
1213
ins.insert(i);
1314
ls.insert(j-i);
1415
i = j++;
1516
}
16-
17-
// update the string and track the length of the longest substring
17+
18+
// update the string and track the length of the longest substring
1819
// by merging/splitting intervals
1920
vector<int> ans;
2021
for (int i = 0; i < queryIndices.size(); ++i) {
@@ -24,41 +25,41 @@ class Solution {
2425
ans.push_back(*ls.rbegin());
2526
continue;
2627
}
27-
28+
2829
// update the ordered sets
2930
// 1. split the involved interval to parts: (left), self, (right)
30-
// ins = 0 3 6
31-
// s = b b b c c c
32-
// query : b
33-
// => ins = 0 3 4 6
34-
// => s = b b b b c c
35-
31+
// ins = 0 3 6
32+
// s = b b b c c c
33+
// query : b
34+
// => ins = 0 3 4 6
35+
// => s = b b b b c c
36+
3637
// find the left-end of the target interval
3738
auto it = prev(ins.upper_bound(queryIndices[i])); // old/left interval
3839
int oldl = *next(it) - *it; // old interval length
3940
ls.erase(ls.find(oldl));
40-
41-
ins.insert(queryIndices[i]); // self interval
41+
42+
ins.insert(queryIndices[i]); // self interval
4243
ins.insert(queryIndices[i] + 1); // right interval
4344
if (*it != queryIndices[i]) ls.insert(queryIndices[i] - *it); // left interval length
4445
ls.insert(1); // self interval length
4546
if (oldl - (queryIndices[i] - *it + 1)) ls.insert(oldl - (queryIndices[i] - *it + 1) ); //right interval length
46-
47+
4748
s[queryIndices[i]] = queryCharacters[i];
4849

4950
// 2. merge the adjacent intervals if the characters are identical
50-
// ins = 0 3 4 6
51-
// s = b b b b c c
52-
// => ins = 0 4 6
53-
51+
// ins = 0 3 4 6
52+
// s = b b b b c c
53+
// => ins = 0 4 6
54+
5455
it = ins.find(queryIndices[i]); // self interval
5556
// merge self and right
5657
if (queryIndices[i] + 1 < n && s[queryIndices[i]] == s[queryIndices[i] + 1]) {
5758
ls.erase(ls.find(*next(it, 2) - *next(it)));
5859
ls.erase(ls.find(1));
5960
ins.erase(next(it));
6061
ls.insert(*next(it) - *it);
61-
}
62+
}
6263
// merge left and self
6364
if (it != ins.begin() && s[queryIndices[i]] == s[queryIndices[i] - 1]) {
6465
ls.erase(ls.find(*next(it) - *it) );
@@ -71,4 +72,4 @@ class Solution {
7172
}
7273
return ans;
7374
}
74-
};
75+
};

0 commit comments

Comments
 (0)