Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions leetcode/1401-1500/1419.Minimum-Number-of-Frogs-Croaking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# [1419.Minimum Number of Frogs Croaking][title]

## Description
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.

Return the minimum number of different frogs to finish all the croaks in the given string.

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`.

**Example 1:**

```
Input: croakOfFrogs = "croakcroak"
Output: 1
Explanation: One frog yelling "croak" twice.
```

**Example 2:**

```
Input: croakOfFrogs = "crcoakroak"
Output: 2
Explanation: The minimum number of frogs is two.
The first frog could yell "crcoakroak".
The second frog could yell later "crcoakroak".
```

**Example 3:**

```
Input: croakOfFrogs = "croakcrook"
Output: -1
Explanation: The given string is an invalid combination of "croak" from different frogs.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/minimum-number-of-frogs-croaking
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(croakOfFrogs string) int {
if croakOfFrogs[0] != 'c' {
return -1
}
indies := map[byte]int{
'c': 0, 'r': 1, 'o': 2, 'a': 3, 'k': 4,
}
ret, free := 0, 0
state := [5]int{}
for _, b := range []byte(croakOfFrogs) {
// croak
switch b {
case 'c':
// 一个新青蛙
if free > 0 {
free--
} else {
ret++
}
state[indies[b]]++
case 'r', 'o', 'a':
if state[indies[b]-1] == 0 {
return -1
}
state[indies[b]-1]--
state[indies[b]]++
case 'k':
if state[indies[b]-1] == 0 {
return -1
}
state[indies[b]-1]--
free++
default:
return -1
}
}
for i := 0; i < 5; i++ {
if state[i] > 0 {
return -1
}
}

return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "croakcroak", 1},
{"TestCase2", "crcoakroak", 2},
{"TestCase3", "croakcrook", -1},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading