-
Notifications
You must be signed in to change notification settings - Fork 0
/
488.go
97 lines (82 loc) · 2.59 KB
/
488.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package p488
/**
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.
Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.
Examples:
Input: "WRRBBW", "RB"
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW
Input: "WWRRBBWW", "WRBRW"
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty
Input:"G", "GGGGG"
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty
Input: "RBYYBBRRB", "YRBGB"
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty
Note:
You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
*/
func findMinStep(board string, hand string) int {
boards := []byte(board)
hands := make(map[byte]int)
for _, v := range []byte(hand) {
hands[v]++
}
ans := findMinStepHelper(boards, hands)
if ans < 0 {
ans = -1
}
return ans
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func findMinStepHelper(boards []byte, hands map[byte]int) int {
length := len(boards)
if length == 0 {
return 0
}
prev := boards[0]
prevIndex := 0
minimun := length << 2
for i := 1; i <= length; i++ {
if i < length && boards[i] == prev {
continue
} else {
step := -10
nextBoards := make([]byte, length-(i-prevIndex))
copy(nextBoards, boards[:prevIndex])
copy(nextBoards[prevIndex:], boards[i:])
if i-prevIndex >= 3 {
step = findMinStepHelper(nextBoards, hands)
} else {
miss := 3 - (i - prevIndex)
if v, ok := hands[prev]; ok && v >= miss {
hands[prev] -= miss
step = miss + findMinStepHelper(nextBoards, hands)
hands[prev] += miss
}
}
if step >= 0 {
minimun = min(minimun, step)
}
if i < length {
prev = boards[i]
prevIndex = i
}
}
}
if minimun == length<<2 {
minimun = -10
}
return minimun
}