Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fcca36

Browse files
author
openset
committed
Add: Maximum Number of Balloons
1 parent c49465b commit 5fcca36

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

‎internal/leetcode/problems_status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,6 @@ var problemStatus = map[int]bool{
220220
1154: true,
221221
1163: true,
222222
1185: true,
223+
1189: true,
223224
1221: true,
224225
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problem_1189
2+
3+
func maxNumberOfBalloons(text string) int {
4+
ans, m := len(text), make(map[byte]int, 5)
5+
for i := 0; i < ans; i++ {
6+
switch text[i] {
7+
case 'b', 'a', 'l', 'o', 'n':
8+
m[text[i]]++
9+
}
10+
}
11+
for _, c := range [...]byte{'b', 'a', 'l', 'o', 'n'} {
12+
if c == 'l' || c == 'o' {
13+
m[c] /= 2
14+
}
15+
if ans > m[c] {
16+
ans = m[c]
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem_1189
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected int
8+
}
9+
10+
func TestMaxNumberOfBalloons(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "nlaebolko",
14+
expected: 1,
15+
},
16+
{
17+
input: "loonbalxballpoon",
18+
expected: 2,
19+
},
20+
{
21+
input: "leetcode",
22+
expected: 0,
23+
},
24+
{
25+
input: "lloo",
26+
expected: 0,
27+
},
28+
}
29+
for _, tc := range tests {
30+
output := maxNumberOfBalloons(tc.input)
31+
if output != tc.expected {
32+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /