Skip to content

Commit e7d820d

Browse files
committed
Add solution and test-cases for problem 1419
1 parent f03226b commit e7d820d

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [1419.Minimum Number of Frogs Croaking][title]
2+
3+
## Description
4+
You are given the string `croakOfFrogs`, which represents a combination of the string `"croak"` from different frogs, that is, multiple frogs can croak at the same time, so multiple `"croak"` are mixed.
5+
6+
Return the minimum number of different frogs to finish all the croaks in the given string.
7+
8+
A valid `"croak"` means a frog is printing five letters `'c'`, `'r'`, `'o'`, `'a'`, and `'k'` **sequentially**. The frogs have to print all five letters to finish a croak. If the given string is not a combination of a valid `"croak"` return `-1`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: croakOfFrogs = "croakcroak"
14+
Output: 1
15+
Explanation: One frog yelling "croak" twice.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: croakOfFrogs = "crcoakroak"
22+
Output: 2
23+
Explanation: The minimum number of frogs is two.
24+
The first frog could yell "crcoakroak".
25+
The second frog could yell later "crcoakroak".
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: croakOfFrogs = "croakcrook"
32+
Output: -1
33+
Explanation: The given string is an invalid combination of "croak" from different frogs.
34+
```
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
39+
40+
[title]: https://leetcode.com/problems/minimum-number-of-frogs-croaking
41+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(croakOfFrogs string) int {
4+
if croakOfFrogs[0] != 'c' {
5+
return -1
6+
}
7+
indies := map[byte]int{
8+
'c': 0, 'r': 1, 'o': 2, 'a': 3, 'k': 4,
9+
}
10+
ret, free := 0, 0
11+
state := [5]int{}
12+
for _, b := range []byte(croakOfFrogs) {
13+
// croak
14+
switch b {
15+
case 'c':
16+
// 一个新青蛙
17+
if free > 0 {
18+
free--
19+
} else {
20+
ret++
21+
}
22+
state[indies[b]]++
23+
case 'r', 'o', 'a':
24+
if state[indies[b]-1] == 0 {
25+
return -1
26+
}
27+
state[indies[b]-1]--
28+
state[indies[b]]++
29+
case 'k':
30+
if state[indies[b]-1] == 0 {
31+
return -1
32+
}
33+
state[indies[b]-1]--
34+
free++
35+
default:
36+
return -1
37+
}
38+
}
39+
for i := 0; i < 5; i++ {
40+
if state[i] > 0 {
41+
return -1
42+
}
43+
}
44+
45+
return ret
546
}

leetcode/1401-1500/1419.Minimum-Number-of-Frogs-Croaking/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "croakcroak", 1},
17+
{"TestCase2", "crcoakroak", 2},
18+
{"TestCase3", "croakcrook", -1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)