-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2213.cpp
More file actions
119 lines (99 loc) · 3.78 KB
/
Copy path2213.cpp
File metadata and controls
119 lines (99 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class Solution {
public:
vector<int> longestRepeating(string s, string queryCharacters, vector<int>& queryIndices) {
multiset<int> lengths;
map<int, int> idx2length;
char curr = '#';
int count = 0;
int start = 0;
int n = s.size();
for (int i = 0; i < n; ++i) {
char c = s[i];
if (c == curr) {
count++;
}
else {
lengths.insert(count);
idx2length[start] = count;
curr = c;
count = 1;
start = i;
}
}
lengths.insert(count);
idx2length[start] = count;
int k = queryCharacters.size();
vector<int> res(k, -1);
for (int i = 0; i < k; ++i) {
char newC = queryCharacters[i];
int targetIdx = queryIndices[i];
if (s[targetIdx] == newC) {
int l = *lengths.rbegin();
res[i] = l;
continue;
}
char left = '#';
char right = '#';
char currC = s[targetIdx];
if (targetIdx > 0) left = s[targetIdx - 1];
if (targetIdx < n - 1) right = s[targetIdx + 1];
auto currP = prev(idx2length.upper_bound(targetIdx));
int currStart = currP->first;
int currLength = currP->second;
int currEnd = currStart + currLength - 1;
idx2length.erase(currP);
lengths.erase(lengths.find(currLength));
// Left part of the original run
int oldLeftLength = targetIdx - currStart;
if (oldLeftLength > 0) {
idx2length[currStart] = oldLeftLength;
lengths.insert(oldLeftLength);
}
// Right part of the original run
int oldRightLength = currEnd - targetIdx;
if (oldRightLength > 0) {
idx2length[targetIdx + 1] = oldRightLength;
lengths.insert(oldRightLength);
}
if (newC != left && newC != right) {
idx2length[targetIdx] = 1;
lengths.insert(1);
}
else if (newC == left && newC == right) {
auto leftP = prev(idx2length.upper_bound(targetIdx - 1));
int leftIdx = leftP->first;
int leftLength = leftP->second;
auto rightP = idx2length.find(targetIdx + 1);
int rightLength = rightP->second;
idx2length.erase(leftP);
idx2length.erase(rightP);
lengths.erase(lengths.find(leftLength));
lengths.erase(lengths.find(rightLength));
int newLength = leftLength + 1 + rightLength;
idx2length[leftIdx] = newLength;
lengths.insert(newLength);
}
else if (newC == left && newC != right) {
auto leftP = prev(idx2length.upper_bound(targetIdx - 1));
int leftIdx = leftP->first;
int leftLength = leftP->second;
idx2length.erase(leftP);
lengths.erase(lengths.find(leftLength));
idx2length[leftIdx] = leftLength + 1;
lengths.insert(leftLength + 1);
}
else if (newC != left && newC == right) {
auto rightP = idx2length.find(targetIdx + 1);
int rightLength = rightP->second;
idx2length.erase(rightP);
lengths.erase(lengths.find(rightLength));
idx2length[targetIdx] = rightLength + 1;
lengths.insert(rightLength + 1);
}
s[targetIdx] = newC;
int l = *lengths.rbegin();
res[i] = l;
}
return res;
}
};