Skip to content

Commit 9f562e9

Browse files
committed
feat: add solutions to lc problem: No.1189
No.1189.Maximum Number of Balloons
1 parent d9ce659 commit 9f562e9

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

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

+51
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ function maxNumberOfBalloons(text: string): number {
107107
};
108108
```
109109

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+
110134
### **C++**
111135

112136
```cpp
@@ -151,6 +175,33 @@ func min(a, b int) int {
151175
}
152176
```
153177

178+
### **Rust**
179+
180+
```rust
181+
impl Solution {
182+
pub fn max_number_of_balloons(text: String) -> i32 {
183+
let mut arr = [0; 5];
184+
for c in text.chars() {
185+
match c {
186+
'b' => arr[0] += 1,
187+
'a' => arr[1] += 1,
188+
'l' => arr[2] += 1,
189+
'o' => arr[3] += 1,
190+
'n' => arr[4] += 1,
191+
_ => {}
192+
}
193+
}
194+
arr[2] /= 2;
195+
arr[3] /= 2;
196+
let mut res = 0;
197+
for num in arr {
198+
res = res.min(num);
199+
}
200+
res
201+
}
202+
}
203+
```
204+
154205
### **...**
155206

156207
```

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

+51
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ function maxNumberOfBalloons(text: string): number {
9898
};
9999
```
100100

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+
101125
### **C++**
102126

103127
```cpp
@@ -142,6 +166,33 @@ func min(a, b int) int {
142166
}
143167
```
144168

169+
### **Rust**
170+
171+
```rust
172+
impl Solution {
173+
pub fn max_number_of_balloons(text: String) -> i32 {
174+
let mut arr = [0; 5];
175+
for c in text.chars() {
176+
match c {
177+
'b' => arr[0] += 1,
178+
'a' => arr[1] += 1,
179+
'l' => arr[2] += 1,
180+
'o' => arr[3] += 1,
181+
'n' => arr[4] += 1,
182+
_ => {}
183+
}
184+
}
185+
arr[2] /= 2;
186+
arr[3] /= 2;
187+
let mut res = 0;
188+
for num in arr {
189+
res = res.min(num);
190+
}
191+
res
192+
}
193+
}
194+
```
195+
145196
### **...**
146197

147198
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn max_number_of_balloons(text: String) -> i32 {
3+
let mut arr = [0; 5];
4+
for c in text.chars() {
5+
match c {
6+
'b' => arr[0] += 1,
7+
'a' => arr[1] += 1,
8+
'l' => arr[2] += 1,
9+
'o' => arr[3] += 1,
10+
'n' => arr[4] += 1,
11+
_ => {}
12+
}
13+
}
14+
arr[2] /= 2;
15+
arr[3] /= 2;
16+
let mut res = 0;
17+
for num in arr {
18+
res = res.min(num);
19+
}
20+
res
21+
}
22+
}

0 commit comments

Comments
 (0)