-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (46 loc) · 921 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
)
func main() {
fmt.Println(minNumberOfFrogs("croakcroak"))
}
func minNumberOfFrogs(croaks string) int {
var (
arr [26][]int
result = -1
values = "croak"
)
for i := 0; i < len(croaks); i++ {
arr[croaks[i]-'a'] = append(arr[croaks[i]-'a'], i)
if isValid(arr) {
var (
diff int
notPrevFrog bool
)
for j := len(values) - 1; j >= 1; j-- {
diff = arr[values[j]-'a'][0] - arr[values[j-1]-'a'][0]
if diff != 1 {
notPrevFrog = true
}
arr[values[j]-'a'] = arr[values[j-1]-'a'][1:]
}
arr[values[0]-'a'] = arr[values[0]-'a'][1:]
if result == -1 {
result = 1
}
if notPrevFrog {
result++
}
fmt.Println(arr)
}
}
return result
}
func isValid(arr [26][]int) bool {
return len(arr['c'-'a']) > 0 &&
len(arr['r'-'a']) > 0 &&
len(arr['o'-'a']) > 0 &&
len(arr['a'-'a']) > 0 &&
len(arr['k'-'a']) > 0
}