diff --git a/internal/leetcode/problems_status.go b/internal/leetcode/problems_status.go index 2ee1ed519..13f1b20dc 100644 --- a/internal/leetcode/problems_status.go +++ b/internal/leetcode/problems_status.go @@ -220,5 +220,6 @@ var problemStatus = map[int]bool{ 1154: true, 1163: true, 1185: true, + 1189: true, 1221: true, } diff --git a/problems/maximum-number-of-balloons/maximum_number_of_balloons.go b/problems/maximum-number-of-balloons/maximum_number_of_balloons.go new file mode 100644 index 000000000..dd734e5c9 --- /dev/null +++ b/problems/maximum-number-of-balloons/maximum_number_of_balloons.go @@ -0,0 +1,20 @@ +package problem_1189 + +func maxNumberOfBalloons(text string) int { + ans, m := len(text), make(map[byte]int, 5) + for i := 0; i < ans; i++ { + switch text[i] { + case 'b', 'a', 'l', 'o', 'n': + m[text[i]]++ + } + } + for _, c := range [...]byte{'b', 'a', 'l', 'o', 'n'} { + if c == 'l' || c == 'o' { + m[c] /= 2 + } + if ans > m[c] { + ans = m[c] + } + } + return ans +} diff --git a/problems/maximum-number-of-balloons/maximum_number_of_balloons_test.go b/problems/maximum-number-of-balloons/maximum_number_of_balloons_test.go new file mode 100644 index 000000000..98ef0d3bd --- /dev/null +++ b/problems/maximum-number-of-balloons/maximum_number_of_balloons_test.go @@ -0,0 +1,35 @@ +package problem_1189 + +import "testing" + +type caseType struct { + input string + expected int +} + +func TestMaxNumberOfBalloons(t *testing.T) { + tests := [...]caseType{ + { + input: "nlaebolko", + expected: 1, + }, + { + input: "loonbalxballpoon", + expected: 2, + }, + { + input: "leetcode", + expected: 0, + }, + { + input: "lloo", + expected: 0, + }, + } + for _, tc := range tests { + output := maxNumberOfBalloons(tc.input) + if output != tc.expected { + t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected) + } + } +}