Skip to content

Commit 6a793d7

Browse files
committed
feat: add solutions to lc problem: No.1405
No.1405.Longest Happy String
1 parent 0c87249 commit 6a793d7

File tree

5 files changed

+370
-2
lines changed

5 files changed

+370
-2
lines changed

solution/1400-1499/1405.Longest Happy String/README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,92 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
贪心,优先选择剩余最多的字符,通过优先队列或排序,确保每次选到的字符都是剩余最多的(为了避免出现连续 3 个一样的字符,一些情况需要选择剩余第二多的字符)
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
class Solution:
65+
def longestDiverseString(self, a: int, b: int, c: int) -> str:
66+
h = []
67+
if a > 0:
68+
heapq.heappush(h, [-a, 'a'])
69+
if b > 0:
70+
heapq.heappush(h, [-b, 'b'])
71+
if c > 0:
72+
heapq.heappush(h, [-c, 'c'])
73+
74+
ans = []
75+
while len(h) > 0:
76+
cur = heapq.heappop(h)
77+
if len(ans) >= 2 and ans[-1] == cur[1] and ans[-2] == cur[1]:
78+
if len(h) == 0:
79+
break
80+
nxt = heapq.heappop(h)
81+
ans.append(nxt[1])
82+
if -nxt[0] > 1:
83+
nxt[0] += 1
84+
heapq.heappush(h, nxt)
85+
heapq.heappush(h, cur)
86+
else:
87+
ans.append(cur[1])
88+
if -cur[0] > 1:
89+
cur[0] += 1
90+
heapq.heappush(h, cur)
91+
92+
93+
return ''.join(ans)
6394
```
6495

6596
### **Java**
6697

6798
<!-- 这里可写当前语言的特殊实现逻辑 -->
6899

69100
```java
101+
class Solution {
102+
public String longestDiverseString(int a, int b, int c) {
103+
Queue<int[]> pq = new PriorityQueue<>((x, y) -> y[1] - x[1]);
104+
if (a > 0) {
105+
pq.offer(new int[]{'a', a});
106+
}
107+
if (b > 0) {
108+
pq.offer(new int[]{'b', b});
109+
}
110+
if (c > 0) {
111+
pq.offer(new int[]{'c', c});
112+
}
70113

114+
StringBuilder sb = new StringBuilder();
115+
while (pq.size() > 0) {
116+
int[] cur = pq.poll();
117+
int n = sb.length();
118+
if (n >= 2 && sb.codePointAt(n - 1) == cur[0] && sb.codePointAt(n - 2) == cur[0]) {
119+
if (pq.size() == 0) {
120+
break;
121+
}
122+
int[] next = pq.poll();
123+
sb.append((char) next[0]);
124+
if (next[1] > 1) {
125+
next[1]--;
126+
pq.offer(next);
127+
}
128+
pq.offer(cur);
129+
} else {
130+
sb.append((char) cur[0]);
131+
if (cur[1] > 1) {
132+
cur[1]--;
133+
pq.offer(cur);
134+
}
135+
}
136+
}
137+
138+
return sb.toString();
139+
}
140+
}
71141
```
72142

73143
### **TypeScript**
@@ -100,6 +170,61 @@ function longestDiverseString(a: number, b: number, c: number): string {
100170
};
101171
```
102172

173+
### **Go**
174+
175+
```go
176+
type pair struct {
177+
c byte
178+
num int
179+
}
180+
181+
type hp []pair
182+
183+
func (a hp) Len() int { return len(a) }
184+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
185+
func (a hp) Less(i, j int) bool { return a[i].num > a[j].num }
186+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) }
187+
func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
188+
189+
func longestDiverseString(a int, b int, c int) string {
190+
var h hp
191+
if a > 0 {
192+
heap.Push(&h, pair{'a', a})
193+
}
194+
if b > 0 {
195+
heap.Push(&h, pair{'b', b})
196+
}
197+
if c > 0 {
198+
heap.Push(&h, pair{'c', c})
199+
}
200+
201+
var ans []byte
202+
for len(h) > 0 {
203+
cur := heap.Pop(&h).(pair)
204+
if len(ans) >= 2 && ans[len(ans)-1] == cur.c && ans[len(ans)-2] == cur.c {
205+
if len(h) == 0 {
206+
break
207+
}
208+
next := heap.Pop(&h).(pair)
209+
ans = append(ans, next.c)
210+
if next.num > 1 {
211+
next.num--
212+
heap.Push(&h, next)
213+
}
214+
heap.Push(&h, cur)
215+
} else {
216+
ans = append(ans, cur.c)
217+
if cur.num > 1 {
218+
cur.num--
219+
heap.Push(&h, cur)
220+
}
221+
}
222+
}
223+
224+
return string(ans)
225+
}
226+
```
227+
103228
### **...**
104229

105230
```

solution/1400-1499/1405.Longest Happy String/README_EN.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,81 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def longestDiverseString(self, a: int, b: int, c: int) -> str:
60+
h = []
61+
if a > 0:
62+
heapq.heappush(h, [-a, 'a'])
63+
if b > 0:
64+
heapq.heappush(h, [-b, 'b'])
65+
if c > 0:
66+
heapq.heappush(h, [-c, 'c'])
67+
68+
ans = []
69+
while len(h) > 0:
70+
cur = heapq.heappop(h)
71+
if len(ans) >= 2 and ans[-1] == cur[1] and ans[-2] == cur[1]:
72+
if len(h) == 0:
73+
break
74+
nxt = heapq.heappop(h)
75+
ans.append(nxt[1])
76+
if -nxt[0] > 1:
77+
nxt[0] += 1
78+
heapq.heappush(h, nxt)
79+
heapq.heappush(h, cur)
80+
else:
81+
ans.append(cur[1])
82+
if -cur[0] > 1:
83+
cur[0] += 1
84+
heapq.heappush(h, cur)
85+
86+
87+
return ''.join(ans)
5988
```
6089

6190
### **Java**
6291

6392
```java
93+
class Solution {
94+
public String longestDiverseString(int a, int b, int c) {
95+
Queue<int[]> pq = new PriorityQueue<>((x, y) -> y[1] - x[1]);
96+
if (a > 0) {
97+
pq.offer(new int[]{'a', a});
98+
}
99+
if (b > 0) {
100+
pq.offer(new int[]{'b', b});
101+
}
102+
if (c > 0) {
103+
pq.offer(new int[]{'c', c});
104+
}
105+
106+
StringBuilder sb = new StringBuilder();
107+
while (pq.size() > 0) {
108+
int[] cur = pq.poll();
109+
int n = sb.length();
110+
if (n >= 2 && sb.codePointAt(n - 1) == cur[0] && sb.codePointAt(n - 2) == cur[0]) {
111+
if (pq.size() == 0) {
112+
break;
113+
}
114+
int[] next = pq.poll();
115+
sb.append((char) next[0]);
116+
if (next[1] > 1) {
117+
next[1]--;
118+
pq.offer(next);
119+
}
120+
pq.offer(cur);
121+
} else {
122+
sb.append((char) cur[0]);
123+
if (cur[1] > 1) {
124+
cur[1]--;
125+
pq.offer(cur);
126+
}
127+
}
128+
}
64129

130+
return sb.toString();
131+
}
132+
}
65133
```
66134

67135
### **TypeScript**
@@ -94,6 +162,61 @@ function longestDiverseString(a: number, b: number, c: number): string {
94162
};
95163
```
96164

165+
### **Go**
166+
167+
```go
168+
type pair struct {
169+
c byte
170+
num int
171+
}
172+
173+
type hp []pair
174+
175+
func (a hp) Len() int { return len(a) }
176+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
177+
func (a hp) Less(i, j int) bool { return a[i].num > a[j].num }
178+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) }
179+
func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
180+
181+
func longestDiverseString(a int, b int, c int) string {
182+
var h hp
183+
if a > 0 {
184+
heap.Push(&h, pair{'a', a})
185+
}
186+
if b > 0 {
187+
heap.Push(&h, pair{'b', b})
188+
}
189+
if c > 0 {
190+
heap.Push(&h, pair{'c', c})
191+
}
192+
193+
var ans []byte
194+
for len(h) > 0 {
195+
cur := heap.Pop(&h).(pair)
196+
if len(ans) >= 2 && ans[len(ans)-1] == cur.c && ans[len(ans)-2] == cur.c {
197+
if len(h) == 0 {
198+
break
199+
}
200+
next := heap.Pop(&h).(pair)
201+
ans = append(ans, next.c)
202+
if next.num > 1 {
203+
next.num--
204+
heap.Push(&h, next)
205+
}
206+
heap.Push(&h, cur)
207+
} else {
208+
ans = append(ans, cur.c)
209+
if cur.num > 1 {
210+
cur.num--
211+
heap.Push(&h, cur)
212+
}
213+
}
214+
}
215+
216+
return string(ans)
217+
}
218+
```
219+
97220
### **...**
98221

99222
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
type pair struct {
2+
c byte
3+
num int
4+
}
5+
6+
type hp []pair
7+
8+
func (a hp) Len() int { return len(a) }
9+
func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
10+
func (a hp) Less(i, j int) bool { return a[i].num > a[j].num }
11+
func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) }
12+
func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
13+
14+
func longestDiverseString(a int, b int, c int) string {
15+
var h hp
16+
if a > 0 {
17+
heap.Push(&h, pair{'a', a})
18+
}
19+
if b > 0 {
20+
heap.Push(&h, pair{'b', b})
21+
}
22+
if c > 0 {
23+
heap.Push(&h, pair{'c', c})
24+
}
25+
26+
var ans []byte
27+
for len(h) > 0 {
28+
cur := heap.Pop(&h).(pair)
29+
if len(ans) >= 2 && ans[len(ans)-1] == cur.c && ans[len(ans)-2] == cur.c {
30+
if len(h) == 0 {
31+
break
32+
}
33+
next := heap.Pop(&h).(pair)
34+
ans = append(ans, next.c)
35+
if next.num > 1 {
36+
next.num--
37+
heap.Push(&h, next)
38+
}
39+
heap.Push(&h, cur)
40+
} else {
41+
ans = append(ans, cur.c)
42+
if cur.num > 1 {
43+
cur.num--
44+
heap.Push(&h, cur)
45+
}
46+
}
47+
}
48+
49+
return string(ans)
50+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public String longestDiverseString(int a, int b, int c) {
3+
Queue<int[]> pq = new PriorityQueue<>((x, y) -> y[1] - x[1]);
4+
if (a > 0) {
5+
pq.offer(new int[]{'a', a});
6+
}
7+
if (b > 0) {
8+
pq.offer(new int[]{'b', b});
9+
}
10+
if (c > 0) {
11+
pq.offer(new int[]{'c', c});
12+
}
13+
14+
StringBuilder sb = new StringBuilder();
15+
while (pq.size() > 0) {
16+
int[] cur = pq.poll();
17+
int n = sb.length();
18+
if (n >= 2 && sb.codePointAt(n - 1) == cur[0] && sb.codePointAt(n - 2) == cur[0]) {
19+
if (pq.size() == 0) {
20+
break;
21+
}
22+
int[] next = pq.poll();
23+
sb.append((char) next[0]);
24+
if (next[1] > 1) {
25+
next[1]--;
26+
pq.offer(next);
27+
}
28+
pq.offer(cur);
29+
} else {
30+
sb.append((char) cur[0]);
31+
if (cur[1] > 1) {
32+
cur[1]--;
33+
pq.offer(cur);
34+
}
35+
}
36+
}
37+
38+
return sb.toString();
39+
}
40+
}

0 commit comments

Comments
 (0)