-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzumaGame.cpp
More file actions
55 lines (51 loc) · 1.5 KB
/
zumaGame.cpp
File metadata and controls
55 lines (51 loc) · 1.5 KB
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
// Source: https://leetcode.com/problems/zuma-game/
// Author: Miao Zhang
// Date: 2021-02-16
class Solution {
public:
int findMinStep(string board, string hand) {
vector<int> h(128, 0);
for (char color: hand) h[color]++;
return dfs(board, h);
}
private:
int dfs(string& board, vector<int>& hand) {
if (board.empty()) return 0;
int res = INT_MAX;
int i = 0;
int j = 0;
while (i < board.size()) {
while (j < board.size() && board[i] == board[j]) j++;
// i ~ j-1 same
char color = board[i];
int b = max(0, 3 - (j - i));
if (hand[color] >= b) {
string newb = update(board.substr(0, i) + board.substr(j));
hand[color] -= b;
int ans = dfs(newb, hand);
if (ans >= 0) {
res = min(res, ans + b);
}
hand[color] += b;
}
i = j;
}
if (update(board).empty()) return res;
return res == INT_MAX ? -1 : res;
}
// "ywwrrrwwyy" -> "ywwwwyy" -> "yyy" -> ""
string update(string board) {
int i = 0;
while (i < board.size()) {
int j = i;
while (j < board.size() && board[i] == board[j]) j++;
if (j - i >= 3) {
board = board.substr(0, i) + board.substr(j);
i = 0;
} else {
i++;
}
}
return board;
}
};