-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
53 lines (52 loc) · 1.42 KB
/
Copy pathsolution.java
File metadata and controls
53 lines (52 loc) · 1.42 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
// 1405. Longest Happy String
// https://leetcode.com/problems/longest-happy-string/
// Medium | Java | Accepted 2026-08-22
// Runtime 2 ms | Memory 42.5 MB
class Solution {
public String longestDiverseString(int a, int b, int c) {
PriorityQueue<int[]> pq = new PriorityQueue<>((d, e)->Integer.compare(e[0], d[0]));
if(a>0)
{
pq.add(new int[]{a, 0});
}
if(b>0)
{
pq.add(new int[]{b, 1});
}
if(c>0)
{
pq.add(new int[]{c, 2});
}
StringBuilder ans = new StringBuilder();
while(!pq.isEmpty())
{
int[] temp = pq.poll();
int n = ans.length();
if(n>=2 && ans.charAt(n-1)==ans.charAt(n-2)&&ans.charAt(n-1)==(temp[1]+'a'))
{
if(pq.isEmpty())
{
break;
}
int[] temp1 = pq.poll();
ans.repeat(temp1[1]+'a', 1);
temp1[0]--;
if(temp1[0]>0)
{
pq.add(temp1);
}
pq.add(temp);
}
else
{
ans.repeat(temp[1]+'a', temp[0]>1 ? 2 : 1);
temp[0]-= temp[0]>1 ? 2 : 1;
if(temp[0]>0)
{
pq.add(temp);
}
}
}
return ans.toString();
}
}