-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0502.py
23 lines (23 loc) · 873 Bytes
/
LC0502.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def longestDiverseString(self, a: int, b: int, c: int) -> str:
res = ""
maxHeap = []
for count, char in [(-a, "a"), (-b, "b"), (-c, "c")]:
if count != 0:
heapq.heappush(maxHeap, (count, char))
while maxHeap:
count, char = heapq.heappop(maxHeap)
if len(res) > 1 and res[-1] == res[-2] == char:
if not maxHeap:
break
count2, char2 = heapq.heappop(maxHeap)
res += char2
count2 += 1 # decrement count but it's negative so +
if count2:
heapq.heappush(maxHeap, (count2, char2))
else:
res += char
count += 1
if count:
heapq.heappush(maxHeap, (count, char))
return res