-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2182.cpp
More file actions
78 lines (64 loc) · 1.86 KB
/
Copy path2182.cpp
File metadata and controls
78 lines (64 loc) · 1.86 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
class Solution {
public:
string repeatLimitedString(const string &s, int limit) const {
int count[27] = {0};
for (const char c : s)
count[c & 0x1F]++;
typedef tuple<char, int> record;
priority_queue<record> pq;
for (int i = 1; i <= 26; i++) {
if (!count[i]) continue;
pq.emplace('`' + i, count[i]);
}
string res;
char last = '~';
while (!empty(pq)) {
const auto [c, cnt] = pq.top();
pq.pop();
if (c == last) {
if (pq.empty()) break;
const auto [oc, ocnt] = pq.top();
pq.pop();
pq.emplace(c, cnt);
res += oc;
if (ocnt > 1) pq.emplace(oc, ocnt - 1);
last = oc;
} else {
res += string(min(cnt, limit), c);
if (cnt > limit) pq.emplace(c, cnt - limit);
last = c;
}
}
return res;
}
};
// O(1) space, no priority_queue
class Solution {
public:
string repeatLimitedString(const string &s, int limit) const {
int count[27] = {0};
for (const char c : s)
count[c & 0x1F]++;
string res;
int i;
for (i = 26; i >= 0; i--)
if (count[i]) break;
if (count[i] == size(s)) return string(min(limit, count[i]), '`' + i);
int j = i - 1;
while (i > 0) {
res += string(min(limit, count[i]), '`' + i);
if (limit < count[i]) {
while (j >= 0 && !count[j])
j--;
if (j < 0) break;
res += '`' + j;
count[i] -= limit;
count[j]--;
} else {
i = j;
j = i - 1;
}
}
return res;
}
};