forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (38 loc) · 1.14 KB
/
Solution.java
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
class Solution {
public String longestDiverseString(int a, int b, int c) {
Queue<int[]> pq = new PriorityQueue<>((x, y) -> y[1] - x[1]);
if (a > 0) {
pq.offer(new int[]{'a', a});
}
if (b > 0) {
pq.offer(new int[]{'b', b});
}
if (c > 0) {
pq.offer(new int[]{'c', c});
}
StringBuilder sb = new StringBuilder();
while (pq.size() > 0) {
int[] cur = pq.poll();
int n = sb.length();
if (n >= 2 && sb.codePointAt(n - 1) == cur[0] && sb.codePointAt(n - 2) == cur[0]) {
if (pq.size() == 0) {
break;
}
int[] next = pq.poll();
sb.append((char) next[0]);
if (next[1] > 1) {
next[1]--;
pq.offer(next);
}
pq.offer(cur);
} else {
sb.append((char) cur[0]);
if (cur[1] > 1) {
cur[1]--;
pq.offer(cur);
}
}
}
return sb.toString();
}
}