Skip to content

Commit 8ef3222

Browse files
committed
feat: add solutions to lc problem: No.0294
No.0294.Flip Game II
1 parent 87da3d6 commit 8ef3222

File tree

8 files changed

+340
-2
lines changed

8 files changed

+340
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
- [公平分发饼干](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README.md) - `DFS``回溯``剪枝`
100100
- [矩阵中的最长递增路径](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README.md) - `DFS``记忆化搜索`
101101
- [网格图中递增路径的数目](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md) - `DFS``记忆化搜索`
102+
- [翻转游戏 II](/solution/0200-0299/0294.Flip%20Game%20II/README.md) - `DFS``状态压缩``记忆化搜索`
102103

103104
<!-- DFS 待补充 -->
104105

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
9797
- [Fair Distribution of Cookies](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README_EN.md) - `DFS`, `Backtracking`
9898
- [Longest Increasing Path in a Matrix](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README_EN.md) - `DFS``Memoization`
9999
- [Number of Increasing Paths in a Grid](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md) - `DFS``Memoization`
100+
- [Flip Game II](/solution/0200-0299/0294.Flip%20Game%20II/README_EN.md) - `DFS``Bitmask``Memoization`
100101

101102
### 4. Dynamic Programming(DP)
102103

solution/0200-0299/0294.Flip Game II/README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,138 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:状态压缩 + 记忆化搜索**
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
5052

5153
<!-- 这里可写当前语言的特殊实现逻辑 -->
5254

5355
```python
54-
56+
class Solution:
57+
def canWin(self, currentState: str) -> bool:
58+
@cache
59+
def dfs(mask):
60+
for i in range(n - 1):
61+
if (mask & (1 << i)) == 0 or (mask & (1 << (i + 1)) == 0):
62+
continue
63+
if dfs(mask ^ (1 << i) ^ (1 << (i + 1))):
64+
continue
65+
return True
66+
return False
67+
68+
mask, n = 0, len(currentState)
69+
for i, c in enumerate(currentState):
70+
if c == '+':
71+
mask |= 1 << i
72+
return dfs(mask)
5573
```
5674

5775
### **Java**
5876

5977
<!-- 这里可写当前语言的特殊实现逻辑 -->
6078

6179
```java
80+
class Solution {
81+
private int n;
82+
private Map<Long, Boolean> memo = new HashMap<>();
83+
84+
public boolean canWin(String currentState) {
85+
long mask = 0;
86+
n = currentState.length();
87+
for (int i = 0; i < n; ++i) {
88+
if (currentState.charAt(i) == '+') {
89+
mask |= 1 << i;
90+
}
91+
}
92+
return dfs(mask);
93+
}
94+
95+
private boolean dfs(long mask) {
96+
if (memo.containsKey(mask)) {
97+
return memo.get(mask);
98+
}
99+
for (int i = 0; i < n - 1; ++i) {
100+
if ((mask & (1 << i)) == 0 || (mask & (1 << (i + 1))) == 0) {
101+
continue;
102+
}
103+
if (dfs(mask ^ (1 << i) ^ (1 << (i + 1)))) {
104+
continue;
105+
}
106+
memo.put(mask, true);
107+
return true;
108+
}
109+
memo.put(mask, false);
110+
return false;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
using ll = long long;
119+
120+
class Solution {
121+
public:
122+
int n;
123+
unordered_map<ll, bool> memo;
124+
125+
bool canWin(string currentState) {
126+
n = currentState.size();
127+
ll mask = 0;
128+
for (int i = 0; i < n; ++i) if (currentState[i] == '+') mask |= 1ll << i;
129+
return dfs(mask);
130+
}
131+
132+
bool dfs(ll mask) {
133+
if (memo.count(mask)) return memo[mask];
134+
for (int i = 0; i < n - 1; ++i)
135+
{
136+
if ((mask & (1ll << i)) == 0 || (mask & (1ll << (i + 1))) == 0) continue;
137+
if (dfs(mask ^ (1ll << i) ^ (1ll << (i + 1)))) continue;
138+
memo[mask] = true;
139+
return true;
140+
}
141+
memo[mask] = false;
142+
return false;
143+
}
144+
};
145+
```
62146
147+
### **Go**
148+
149+
```go
150+
func canWin(currentState string) bool {
151+
n := len(currentState)
152+
memo := map[int]bool{}
153+
mask := 0
154+
for i, c := range currentState {
155+
if c == '+' {
156+
mask |= 1 << i
157+
}
158+
}
159+
var dfs func(int) bool
160+
dfs = func(mask int) bool {
161+
if v, ok := memo[mask]; ok {
162+
return v
163+
}
164+
for i := 0; i < n-1; i++ {
165+
if (mask&(1<<i)) == 0 || (mask&(1<<(i+1))) == 0 {
166+
continue
167+
}
168+
if dfs(mask ^ (1 << i) ^ (1 << (i + 1))) {
169+
continue
170+
}
171+
memo[mask] = true
172+
return true
173+
}
174+
memo[mask] = false
175+
return false
176+
}
177+
return dfs(mask)
178+
}
63179
```
64180

65181
### **...**

solution/0200-0299/0294.Flip Game II/README_EN.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,127 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def canWin(self, currentState: str) -> bool:
49+
@cache
50+
def dfs(mask):
51+
for i in range(n - 1):
52+
if (mask & (1 << i)) == 0 or (mask & (1 << (i + 1)) == 0):
53+
continue
54+
if dfs(mask ^ (1 << i) ^ (1 << (i + 1))):
55+
continue
56+
return True
57+
return False
58+
59+
mask, n = 0, len(currentState)
60+
for i, c in enumerate(currentState):
61+
if c == '+':
62+
mask |= 1 << i
63+
return dfs(mask)
4864
```
4965

5066
### **Java**
5167

5268
```java
69+
class Solution {
70+
private int n;
71+
private Map<Long, Boolean> memo = new HashMap<>();
72+
73+
public boolean canWin(String currentState) {
74+
long mask = 0;
75+
n = currentState.length();
76+
for (int i = 0; i < n; ++i) {
77+
if (currentState.charAt(i) == '+') {
78+
mask |= 1 << i;
79+
}
80+
}
81+
return dfs(mask);
82+
}
83+
84+
private boolean dfs(long mask) {
85+
if (memo.containsKey(mask)) {
86+
return memo.get(mask);
87+
}
88+
for (int i = 0; i < n - 1; ++i) {
89+
if ((mask & (1 << i)) == 0 || (mask & (1 << (i + 1))) == 0) {
90+
continue;
91+
}
92+
if (dfs(mask ^ (1 << i) ^ (1 << (i + 1)))) {
93+
continue;
94+
}
95+
memo.put(mask, true);
96+
return true;
97+
}
98+
memo.put(mask, false);
99+
return false;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
using ll = long long;
108+
109+
class Solution {
110+
public:
111+
int n;
112+
unordered_map<ll, bool> memo;
113+
114+
bool canWin(string currentState) {
115+
n = currentState.size();
116+
ll mask = 0;
117+
for (int i = 0; i < n; ++i) if (currentState[i] == '+') mask |= 1ll << i;
118+
return dfs(mask);
119+
}
120+
121+
bool dfs(ll mask) {
122+
if (memo.count(mask)) return memo[mask];
123+
for (int i = 0; i < n - 1; ++i)
124+
{
125+
if ((mask & (1ll << i)) == 0 || (mask & (1ll << (i + 1))) == 0) continue;
126+
if (dfs(mask ^ (1ll << i) ^ (1ll << (i + 1)))) continue;
127+
memo[mask] = true;
128+
return true;
129+
}
130+
memo[mask] = false;
131+
return false;
132+
}
133+
};
134+
```
53135
136+
### **Go**
137+
138+
```go
139+
func canWin(currentState string) bool {
140+
n := len(currentState)
141+
memo := map[int]bool{}
142+
mask := 0
143+
for i, c := range currentState {
144+
if c == '+' {
145+
mask |= 1 << i
146+
}
147+
}
148+
var dfs func(int) bool
149+
dfs = func(mask int) bool {
150+
if v, ok := memo[mask]; ok {
151+
return v
152+
}
153+
for i := 0; i < n-1; i++ {
154+
if (mask&(1<<i)) == 0 || (mask&(1<<(i+1))) == 0 {
155+
continue
156+
}
157+
if dfs(mask ^ (1 << i) ^ (1 << (i + 1))) {
158+
continue
159+
}
160+
memo[mask] = true
161+
return true
162+
}
163+
memo[mask] = false
164+
return false
165+
}
166+
return dfs(mask)
167+
}
54168
```
55169

56170
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using ll = long long;
2+
3+
class Solution {
4+
public:
5+
int n;
6+
unordered_map<ll, bool> memo;
7+
8+
bool canWin(string currentState) {
9+
n = currentState.size();
10+
ll mask = 0;
11+
for (int i = 0; i < n; ++i) if (currentState[i] == '+') mask |= 1ll << i;
12+
return dfs(mask);
13+
}
14+
15+
bool dfs(ll mask) {
16+
if (memo.count(mask)) return memo[mask];
17+
for (int i = 0; i < n - 1; ++i)
18+
{
19+
if ((mask & (1ll << i)) == 0 || (mask & (1ll << (i + 1))) == 0) continue;
20+
if (dfs(mask ^ (1ll << i) ^ (1ll << (i + 1)))) continue;
21+
memo[mask] = true;
22+
return true;
23+
}
24+
memo[mask] = false;
25+
return false;
26+
}
27+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func canWin(currentState string) bool {
2+
n := len(currentState)
3+
memo := map[int]bool{}
4+
mask := 0
5+
for i, c := range currentState {
6+
if c == '+' {
7+
mask |= 1 << i
8+
}
9+
}
10+
var dfs func(int) bool
11+
dfs = func(mask int) bool {
12+
if v, ok := memo[mask]; ok {
13+
return v
14+
}
15+
for i := 0; i < n-1; i++ {
16+
if (mask&(1<<i)) == 0 || (mask&(1<<(i+1))) == 0 {
17+
continue
18+
}
19+
if dfs(mask ^ (1 << i) ^ (1 << (i + 1))) {
20+
continue
21+
}
22+
memo[mask] = true
23+
return true
24+
}
25+
memo[mask] = false
26+
return false
27+
}
28+
return dfs(mask)
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
private int n;
3+
private Map<Long, Boolean> memo = new HashMap<>();
4+
5+
public boolean canWin(String currentState) {
6+
long mask = 0;
7+
n = currentState.length();
8+
for (int i = 0; i < n; ++i) {
9+
if (currentState.charAt(i) == '+') {
10+
mask |= 1 << i;
11+
}
12+
}
13+
return dfs(mask);
14+
}
15+
16+
private boolean dfs(long mask) {
17+
if (memo.containsKey(mask)) {
18+
return memo.get(mask);
19+
}
20+
for (int i = 0; i < n - 1; ++i) {
21+
if ((mask & (1 << i)) == 0 || (mask & (1 << (i + 1))) == 0) {
22+
continue;
23+
}
24+
if (dfs(mask ^ (1 << i) ^ (1 << (i + 1)))) {
25+
continue;
26+
}
27+
memo.put(mask, true);
28+
return true;
29+
}
30+
memo.put(mask, false);
31+
return false;
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def canWin(self, currentState: str) -> bool:
3+
@cache
4+
def dfs(mask):
5+
for i in range(n - 1):
6+
if (mask & (1 << i)) == 0 or (mask & (1 << (i + 1)) == 0):
7+
continue
8+
if dfs(mask ^ (1 << i) ^ (1 << (i + 1))):
9+
continue
10+
return True
11+
return False
12+
13+
mask, n = 0, len(currentState)
14+
for i, c in enumerate(currentState):
15+
if c == '+':
16+
mask |= 1 << i
17+
return dfs(mask)

0 commit comments

Comments
 (0)