forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1405-longest-happy-string.kt
More file actions
32 lines (28 loc) · 911 Bytes
/
1405-longest-happy-string.kt
File metadata and controls
32 lines (28 loc) · 911 Bytes
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
class Solution {
fun longestDiverseString(a: Int, b: Int, c: Int): String {
val maxHeap = PriorityQueue<Pair<Char, Int>> { a, b ->
b.second - a.second
}.apply {
if (a > 0) add('a' to a)
if (b > 0) add('b' to b)
if (c > 0) add('c' to c)
}
val res = StringBuilder()
while (maxHeap.isNotEmpty()) {
var (c, v) = maxHeap.poll()
var n = res.length
if (n > 1 && res.get(n - 1) == c && res.get(n - 2) == c) {
if (maxHeap.isEmpty()) break
var (c2, v2) = maxHeap.poll()
res.append(c2)
v2--
if (v2 > 0) maxHeap.add(c2 to v2)
} else {
res.append(c)
v--
}
if (v > 0) maxHeap.add(c to v)
}
return res.toString()
}
}