Skip to content

Commit 8f5467c

Browse files
committed
feat: add solutions to lc problem: No.1189
No.1189.Maximum Number of Balloons
1 parent 3c1a1fb commit 8f5467c

File tree

7 files changed

+124
-199
lines changed

7 files changed

+124
-199
lines changed

solution/1100-1199/1189.Maximum Number of Balloons/README.md

Lines changed: 48 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
简单计数。
50+
**方法一:计数**
51+
52+
我们统计字符串 `text` 中每个字母出现的次数,然后将其中字母 `'o'``'l'` 的出现次数分别除以 2,这是因为单词 `balloon` 中字母 `'o'``'l'` 都出现了 2 次。
53+
54+
接着,我们遍历单词 `balon` 中的每个字母,统计每个字母在字符串 `text` 中出现的次数的最小值,这个最小值就是单词 `balloon` 在字符串 `text` 中出现的最大次数。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 `text` 的长度;而 $C$ 为字符集大小,本题中 $C = 26$。
5157

5258
<!-- tabs:start -->
5359

@@ -58,10 +64,10 @@
5864
```python
5965
class Solution:
6066
def maxNumberOfBalloons(self, text: str) -> int:
61-
counter = Counter(text)
62-
counter['l'] >>= 1
63-
counter['o'] >>= 1
64-
return min(counter[c] for c in 'balon')
67+
cnt = Counter(text)
68+
cnt['o'] >>= 1
69+
cnt['l'] >>= 1
70+
return min(cnt[c] for c in 'balon')
6571
```
6672

6773
### **Java**
@@ -71,79 +77,38 @@ class Solution:
7177
```java
7278
class Solution {
7379
public int maxNumberOfBalloons(String text) {
74-
int[] counter = new int[26];
75-
for (char c : text.toCharArray()) {
76-
++counter[c - 'a'];
80+
int[] cnt = new int[26];
81+
for (int i = 0; i < text.length(); ++i) {
82+
++cnt[text.charAt(i) - 'a'];
7783
}
78-
counter['l' - 'a'] >>= 1;
79-
counter['o' - 'a'] >>= 1;
80-
int ans = 10000;
84+
cnt['l' - 'a'] >>= 1;
85+
cnt['o' - 'a'] >>= 1;
86+
int ans = 1 << 30;
8187
for (char c : "balon".toCharArray()) {
82-
ans = Math.min(ans, counter[c - 'a']);
88+
ans = Math.min(ans, cnt[c - 'a']);
8389
}
8490
return ans;
8591
}
8692
}
8793
```
8894

89-
### **TypeScript**
90-
91-
```ts
92-
function maxNumberOfBalloons(text: string): number {
93-
let targets: Set<string> = new Set('balloon'.split(''));
94-
let cnt = new Array(126).fill(0);
95-
for (let char of text) {
96-
if (targets.has(char)) {
97-
cnt[char.charCodeAt(0)]++;
98-
}
99-
}
100-
cnt['l'.charCodeAt(0)] >>= 1;
101-
cnt['o'.charCodeAt(0)] >>= 1;
102-
let ans = Number.MAX_SAFE_INTEGER;
103-
for (let char of targets) {
104-
ans = Math.min(cnt[char.charCodeAt(0)], ans);
105-
}
106-
return ans;
107-
}
108-
```
109-
110-
```ts
111-
function maxNumberOfBalloons(text: string): number {
112-
const map = new Map([
113-
['b', 0],
114-
['a', 0],
115-
['l', 0],
116-
['o', 0],
117-
['n', 0],
118-
]);
119-
for (const c of text) {
120-
if (map.has(c)) {
121-
map.set(c, map.get(c) + 1);
122-
}
123-
}
124-
map.set('l', Math.floor(map.get('l') / 2));
125-
map.set('o', Math.floor(map.get('o') / 2));
126-
let res = Infinity;
127-
for (const value of map.values()) {
128-
res = Math.min(res, value);
129-
}
130-
return res;
131-
}
132-
```
133-
13495
### **C++**
13596

13697
```cpp
13798
class Solution {
13899
public:
139100
int maxNumberOfBalloons(string text) {
140-
vector<int> counter(26);
141-
for (char& c : text) ++counter[c - 'a'];
142-
counter['l' - 'a'] >>= 1;
143-
counter['o' - 'a'] >>= 1;
144-
int ans = 10000;
101+
int cnt[26]{};
102+
for (char c : text) {
103+
++cnt[c - 'a'];
104+
}
105+
cnt['o' - 'a'] >>= 1;
106+
cnt['l' - 'a'] >>= 1;
107+
int ans = 1 << 30;
145108
string t = "balon";
146-
for (char& c : t) ans = min(ans, counter[c - 'a']);
109+
for (char c : t) {
110+
ans = min(ans, cnt[c - 'a']);
111+
}
147112
return ans;
148113
}
149114
};
@@ -153,25 +118,31 @@ public:
153118
154119
```go
155120
func maxNumberOfBalloons(text string) int {
156-
counter := make([]int, 26)
157-
for i := range text {
158-
counter[text[i]-'a']++
121+
cnt := [26]int{}
122+
for _, c := range text {
123+
cnt[c-'a']++
159124
}
160-
counter['l'-'a'] >>= 1
161-
counter['o'-'a'] >>= 1
162-
ans := 10000
163-
t := "balon"
164-
for i := range t {
165-
ans = min(ans, counter[t[i]-'a'])
125+
cnt['l'-'a'] >>= 1
126+
cnt['o'-'a'] >>= 1
127+
ans := 1 << 30
128+
for _, c := range "balon" {
129+
if x := cnt[c-'a']; ans > x {
130+
ans = x
131+
}
166132
}
167133
return ans
168134
}
135+
```
169136

170-
func min(a, b int) int {
171-
if a < b {
172-
return a
173-
}
174-
return b
137+
### **TypeScript**
138+
139+
```ts
140+
function maxNumberOfBalloons(text: string): number {
141+
const cnt = new Array(26).fill(0);
142+
for (const c of text) {
143+
cnt[c.charCodeAt(0) - 97]++;
144+
}
145+
return Math.min(cnt[0], cnt[1], cnt[11] >> 1, cnt[14] >> 1, cnt[13]);
175146
}
176147
```
177148

solution/1100-1199/1189.Maximum Number of Balloons/README_EN.md

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -51,90 +51,49 @@
5151
```python
5252
class Solution:
5353
def maxNumberOfBalloons(self, text: str) -> int:
54-
counter = Counter(text)
55-
counter['l'] >>= 1
56-
counter['o'] >>= 1
57-
return min(counter[c] for c in 'balon')
54+
cnt = Counter(text)
55+
cnt['o'] >>= 1
56+
cnt['l'] >>= 1
57+
return min(cnt[c] for c in 'balon')
5858
```
5959

6060
### **Java**
6161

6262
```java
6363
class Solution {
6464
public int maxNumberOfBalloons(String text) {
65-
int[] counter = new int[26];
66-
for (char c : text.toCharArray()) {
67-
++counter[c - 'a'];
65+
int[] cnt = new int[26];
66+
for (int i = 0; i < text.length(); ++i) {
67+
++cnt[text.charAt(i) - 'a'];
6868
}
69-
counter['l' - 'a'] >>= 1;
70-
counter['o' - 'a'] >>= 1;
71-
int ans = 10000;
69+
cnt['l' - 'a'] >>= 1;
70+
cnt['o' - 'a'] >>= 1;
71+
int ans = 1 << 30;
7272
for (char c : "balon".toCharArray()) {
73-
ans = Math.min(ans, counter[c - 'a']);
73+
ans = Math.min(ans, cnt[c - 'a']);
7474
}
7575
return ans;
7676
}
7777
}
7878
```
7979

80-
### **TypeScript**
81-
82-
```ts
83-
function maxNumberOfBalloons(text: string): number {
84-
let targets: Set<string> = new Set('balloon'.split(''));
85-
let cnt = new Array(126).fill(0);
86-
for (let char of text) {
87-
if (targets.has(char)) {
88-
cnt[char.charCodeAt(0)]++;
89-
}
90-
}
91-
cnt['l'.charCodeAt(0)] >>= 1;
92-
cnt['o'.charCodeAt(0)] >>= 1;
93-
let ans = Number.MAX_SAFE_INTEGER;
94-
for (let char of targets) {
95-
ans = Math.min(cnt[char.charCodeAt(0)], ans);
96-
}
97-
return ans;
98-
}
99-
```
100-
101-
```ts
102-
function maxNumberOfBalloons(text: string): number {
103-
const map = new Map([
104-
['b', 0],
105-
['a', 0],
106-
['l', 0],
107-
['o', 0],
108-
['n', 0],
109-
]);
110-
for (const c of text) {
111-
if (map.has(c)) {
112-
map.set(c, map.get(c) + 1);
113-
}
114-
}
115-
map.set('l', Math.floor(map.get('l') / 2));
116-
map.set('o', Math.floor(map.get('o') / 2));
117-
let res = Infinity;
118-
for (const value of map.values()) {
119-
res = Math.min(res, value);
120-
}
121-
return res;
122-
}
123-
```
124-
12580
### **C++**
12681

12782
```cpp
12883
class Solution {
12984
public:
13085
int maxNumberOfBalloons(string text) {
131-
vector<int> counter(26);
132-
for (char& c : text) ++counter[c - 'a'];
133-
counter['l' - 'a'] >>= 1;
134-
counter['o' - 'a'] >>= 1;
135-
int ans = 10000;
86+
int cnt[26]{};
87+
for (char c : text) {
88+
++cnt[c - 'a'];
89+
}
90+
cnt['o' - 'a'] >>= 1;
91+
cnt['l' - 'a'] >>= 1;
92+
int ans = 1 << 30;
13693
string t = "balon";
137-
for (char& c : t) ans = min(ans, counter[c - 'a']);
94+
for (char c : t) {
95+
ans = min(ans, cnt[c - 'a']);
96+
}
13897
return ans;
13998
}
14099
};
@@ -144,25 +103,31 @@ public:
144103
145104
```go
146105
func maxNumberOfBalloons(text string) int {
147-
counter := make([]int, 26)
148-
for i := range text {
149-
counter[text[i]-'a']++
106+
cnt := [26]int{}
107+
for _, c := range text {
108+
cnt[c-'a']++
150109
}
151-
counter['l'-'a'] >>= 1
152-
counter['o'-'a'] >>= 1
153-
ans := 10000
154-
t := "balon"
155-
for i := range t {
156-
ans = min(ans, counter[t[i]-'a'])
110+
cnt['l'-'a'] >>= 1
111+
cnt['o'-'a'] >>= 1
112+
ans := 1 << 30
113+
for _, c := range "balon" {
114+
if x := cnt[c-'a']; ans > x {
115+
ans = x
116+
}
157117
}
158118
return ans
159119
}
120+
```
160121

161-
func min(a, b int) int {
162-
if a < b {
163-
return a
164-
}
165-
return b
122+
### **TypeScript**
123+
124+
```ts
125+
function maxNumberOfBalloons(text: string): number {
126+
const cnt = new Array(26).fill(0);
127+
for (const c of text) {
128+
cnt[c.charCodeAt(0) - 97]++;
129+
}
130+
return Math.min(cnt[0], cnt[1], cnt[11] >> 1, cnt[14] >> 1, cnt[13]);
166131
}
167132
```
168133

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
class Solution {
22
public:
33
int maxNumberOfBalloons(string text) {
4-
vector<int> counter(26);
5-
for (char& c : text) ++counter[c - 'a'];
6-
counter['l' - 'a'] >>= 1;
7-
counter['o' - 'a'] >>= 1;
8-
int ans = 10000;
4+
int cnt[26]{};
5+
for (char c : text) {
6+
++cnt[c - 'a'];
7+
}
8+
cnt['o' - 'a'] >>= 1;
9+
cnt['l' - 'a'] >>= 1;
10+
int ans = 1 << 30;
911
string t = "balon";
10-
for (char& c : t) ans = min(ans, counter[c - 'a']);
12+
for (char c : t) {
13+
ans = min(ans, cnt[c - 'a']);
14+
}
1115
return ans;
1216
}
1317
};
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
func maxNumberOfBalloons(text string) int {
2-
counter := make([]int, 26)
3-
for i := range text {
4-
counter[text[i]-'a']++
2+
cnt := [26]int{}
3+
for _, c := range text {
4+
cnt[c-'a']++
55
}
6-
counter['l'-'a'] >>= 1
7-
counter['o'-'a'] >>= 1
8-
ans := 10000
9-
t := "balon"
10-
for i := range t {
11-
ans = min(ans, counter[t[i]-'a'])
6+
cnt['l'-'a'] >>= 1
7+
cnt['o'-'a'] >>= 1
8+
ans := 1 << 30
9+
for _, c := range "balon" {
10+
if x := cnt[c-'a']; ans > x {
11+
ans = x
12+
}
1213
}
1314
return ans
14-
}
15-
16-
func min(a, b int) int {
17-
if a < b {
18-
return a
19-
}
20-
return b
2115
}

0 commit comments

Comments
 (0)