Skip to content

Commit 6614109

Browse files
authored
feat: add solutions to lc problems: No.1706,1707 (doocs#2132)
* No.1706.Where Will the Ball Fall * No.1707.Maximum XOR With an Element From Array
1 parent bf6ea02 commit 6614109

File tree

14 files changed

+625
-407
lines changed

14 files changed

+625
-407
lines changed

solution/1700-1799/1706.Where Will the Ball Fall/README.md

+36-31
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V"
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69-
球被卡住共有 4 种情况:
69+
**方法一:分情况讨论 + DFS**
70+
71+
我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $dfs(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住:
7072

7173
1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧
7274
1. 球位于最右一列,并且此单元格挡板将球导向右侧
7375
1. 球所在的单元格挡板将球导向右侧,并且球右侧相邻单元格挡板将球导向左侧
7476
1. 球所在的单元格挡板将球导向左侧,并且球左侧相邻单元格挡板将球导向右侧
7577

76-
DFS 搜索即可。
78+
如果满足以上任意一种情况,我们就可以判断球会卡住,返回 $-1$。否则,我们就可以继续递归地寻找球的下一个位置。最后,如果球到了最后一行,我们就可以返回当前列的编号。
79+
80+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $grid$ 的行数和列数。
7781

7882
<!-- tabs:start -->
7983

@@ -84,10 +88,7 @@ DFS 搜索即可。
8488
```python
8589
class Solution:
8690
def findBall(self, grid: List[List[int]]) -> List[int]:
87-
m, n = len(grid), len(grid[0])
88-
89-
def dfs(i, j):
90-
nonlocal m, n
91+
def dfs(i: int, j: int) -> int:
9192
if i == m:
9293
return j
9394
if j == 0 and grid[i][j] == -1:
@@ -100,6 +101,7 @@ class Solution:
100101
return -1
101102
return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)
102103

104+
m, n = len(grid), len(grid[0])
103105
return [dfs(0, j) for j in range(n)]
104106
```
105107

@@ -150,35 +152,40 @@ class Solution {
150152
```cpp
151153
class Solution {
152154
public:
153-
int m, n;
154-
vector<vector<int>> grid;
155-
156155
vector<int> findBall(vector<vector<int>>& grid) {
157-
this->grid = grid;
158-
m = grid.size();
159-
n = grid[0].size();
156+
int m = grid.size(), n = grid[0].size();
160157
vector<int> ans(n);
161-
for (int j = 0; j < n; ++j) ans[j] = dfs(0, j);
158+
function<int(int, int)> dfs = [&](int i, int j) {
159+
if (i == m) {
160+
return j;
161+
}
162+
if (j == 0 && grid[i][j] == -1) {
163+
return -1;
164+
}
165+
if (j == n - 1 && grid[i][j] == 1) {
166+
return -1;
167+
}
168+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) {
169+
return -1;
170+
}
171+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) {
172+
return -1;
173+
}
174+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
175+
};
176+
for (int j = 0; j < n; ++j) {
177+
ans[j] = dfs(0, j);
178+
}
162179
return ans;
163180
}
164-
165-
int dfs(int i, int j) {
166-
if (i == m) return j;
167-
if (j == 0 && grid[i][j] == -1) return -1;
168-
if (j == n - 1 && grid[i][j] == 1) return -1;
169-
if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1;
170-
if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1;
171-
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
172-
}
173181
};
174182
```
175183
176184
### **Go**
177185
178186
```go
179-
func findBall(grid [][]int) []int {
187+
func findBall(grid [][]int) (ans []int) {
180188
m, n := len(grid), len(grid[0])
181-
182189
var dfs func(i, j int) int
183190
dfs = func(i, j int) int {
184191
if i == m {
@@ -201,12 +208,10 @@ func findBall(grid [][]int) []int {
201208
}
202209
return dfs(i+1, j-1)
203210
}
204-
205-
var ans []int
206211
for j := 0; j < n; j++ {
207212
ans = append(ans, dfs(0, j))
208213
}
209-
return ans
214+
return
210215
}
211216
```
212217

@@ -216,7 +221,6 @@ func findBall(grid [][]int) []int {
216221
function findBall(grid: number[][]): number[] {
217222
const m = grid.length;
218223
const n = grid[0].length;
219-
const res = new Array(n).fill(0);
220224
const dfs = (i: number, j: number) => {
221225
if (i === m) {
222226
return j;
@@ -233,10 +237,11 @@ function findBall(grid: number[][]): number[] {
233237
return dfs(i + 1, j - 1);
234238
}
235239
};
236-
for (let i = 0; i < n; i++) {
237-
res[i] = dfs(0, i);
240+
const ans: number[] = [];
241+
for (let j = 0; j < n; ++j) {
242+
ans.push(dfs(0, j));
238243
}
239-
return res;
244+
return ans;
240245
}
241246
```
242247

solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md

+43-29
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,27 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an
6060

6161
## Solutions
6262

63+
**Solution 1: Case Discussion + DFS**
64+
65+
We can use DFS to simulate the movement of the ball. We design a function $dfs(i, j)$, which represents that the ball starts from the $i$th row and the $j$th column, and finally falls in which column. The ball will get stuck in the following situations:
66+
67+
1. The ball is in the leftmost column, and the cell's vane directs the ball to the left.
68+
2. The ball is in the rightmost column, and the cell's vane directs the ball to the right.
69+
3. The cell's vane where the ball is located directs the ball to the right, and the vane of the cell adjacent to the right of the ball directs the ball to the left.
70+
4. The cell's vane where the ball is located directs the ball to the left, and the vane of the cell adjacent to the left of the ball directs the ball to the right.
71+
72+
If any of the above situations are met, we can judge that the ball will get stuck and return $-1$. Otherwise, we can continue to recursively find the next position of the ball. Finally, if the ball reaches the last row, we can return the current column number.
73+
74+
The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Where $m$ and $n$ are the number of rows and columns of the array $grid$, respectively.
75+
6376
<!-- tabs:start -->
6477

6578
### **Python3**
6679

6780
```python
6881
class Solution:
6982
def findBall(self, grid: List[List[int]]) -> List[int]:
70-
m, n = len(grid), len(grid[0])
71-
72-
def dfs(i, j):
73-
nonlocal m, n
83+
def dfs(i: int, j: int) -> int:
7484
if i == m:
7585
return j
7686
if j == 0 and grid[i][j] == -1:
@@ -83,6 +93,7 @@ class Solution:
8393
return -1
8494
return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)
8595

96+
m, n = len(grid), len(grid[0])
8697
return [dfs(0, j) for j in range(n)]
8798
```
8899

@@ -131,35 +142,40 @@ class Solution {
131142
```cpp
132143
class Solution {
133144
public:
134-
int m, n;
135-
vector<vector<int>> grid;
136-
137145
vector<int> findBall(vector<vector<int>>& grid) {
138-
this->grid = grid;
139-
m = grid.size();
140-
n = grid[0].size();
146+
int m = grid.size(), n = grid[0].size();
141147
vector<int> ans(n);
142-
for (int j = 0; j < n; ++j) ans[j] = dfs(0, j);
148+
function<int(int, int)> dfs = [&](int i, int j) {
149+
if (i == m) {
150+
return j;
151+
}
152+
if (j == 0 && grid[i][j] == -1) {
153+
return -1;
154+
}
155+
if (j == n - 1 && grid[i][j] == 1) {
156+
return -1;
157+
}
158+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) {
159+
return -1;
160+
}
161+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) {
162+
return -1;
163+
}
164+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
165+
};
166+
for (int j = 0; j < n; ++j) {
167+
ans[j] = dfs(0, j);
168+
}
143169
return ans;
144170
}
145-
146-
int dfs(int i, int j) {
147-
if (i == m) return j;
148-
if (j == 0 && grid[i][j] == -1) return -1;
149-
if (j == n - 1 && grid[i][j] == 1) return -1;
150-
if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1;
151-
if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1;
152-
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
153-
}
154171
};
155172
```
156173
157174
### **Go**
158175
159176
```go
160-
func findBall(grid [][]int) []int {
177+
func findBall(grid [][]int) (ans []int) {
161178
m, n := len(grid), len(grid[0])
162-
163179
var dfs func(i, j int) int
164180
dfs = func(i, j int) int {
165181
if i == m {
@@ -182,12 +198,10 @@ func findBall(grid [][]int) []int {
182198
}
183199
return dfs(i+1, j-1)
184200
}
185-
186-
var ans []int
187201
for j := 0; j < n; j++ {
188202
ans = append(ans, dfs(0, j))
189203
}
190-
return ans
204+
return
191205
}
192206
```
193207

@@ -197,7 +211,6 @@ func findBall(grid [][]int) []int {
197211
function findBall(grid: number[][]): number[] {
198212
const m = grid.length;
199213
const n = grid[0].length;
200-
const res = new Array(n).fill(0);
201214
const dfs = (i: number, j: number) => {
202215
if (i === m) {
203216
return j;
@@ -214,10 +227,11 @@ function findBall(grid: number[][]): number[] {
214227
return dfs(i + 1, j - 1);
215228
}
216229
};
217-
for (let i = 0; i < n; i++) {
218-
res[i] = dfs(0, i);
230+
const ans: number[] = [];
231+
for (let j = 0; j < n; ++j) {
232+
ans.push(dfs(0, j));
219233
}
220-
return res;
234+
return ans;
221235
}
222236
```
223237

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
class Solution {
2-
public:
3-
int m, n;
4-
vector<vector<int>> grid;
5-
6-
vector<int> findBall(vector<vector<int>>& grid) {
7-
this->grid = grid;
8-
m = grid.size();
9-
n = grid[0].size();
10-
vector<int> ans(n);
11-
for (int j = 0; j < n; ++j) ans[j] = dfs(0, j);
12-
return ans;
13-
}
14-
15-
int dfs(int i, int j) {
16-
if (i == m) return j;
17-
if (j == 0 && grid[i][j] == -1) return -1;
18-
if (j == n - 1 && grid[i][j] == 1) return -1;
19-
if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1;
20-
if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1;
21-
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
22-
}
1+
class Solution {
2+
public:
3+
vector<int> findBall(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<int> ans(n);
6+
function<int(int, int)> dfs = [&](int i, int j) {
7+
if (i == m) {
8+
return j;
9+
}
10+
if (j == 0 && grid[i][j] == -1) {
11+
return -1;
12+
}
13+
if (j == n - 1 && grid[i][j] == 1) {
14+
return -1;
15+
}
16+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) {
17+
return -1;
18+
}
19+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) {
20+
return -1;
21+
}
22+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
23+
};
24+
for (int j = 0; j < n; ++j) {
25+
ans[j] = dfs(0, j);
26+
}
27+
return ans;
28+
}
2329
};

solution/1700-1799/1706.Where Will the Ball Fall/Solution.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
func findBall(grid [][]int) []int {
1+
func findBall(grid [][]int) (ans []int) {
22
m, n := len(grid), len(grid[0])
3-
43
var dfs func(i, j int) int
54
dfs = func(i, j int) int {
65
if i == m {
@@ -23,10 +22,8 @@ func findBall(grid [][]int) []int {
2322
}
2423
return dfs(i+1, j-1)
2524
}
26-
27-
var ans []int
2825
for j := 0; j < n; j++ {
2926
ans = append(ans, dfs(0, j))
3027
}
31-
return ans
28+
return
3229
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
class Solution:
2-
def findBall(self, grid: List[List[int]]) -> List[int]:
3-
m, n = len(grid), len(grid[0])
4-
5-
def dfs(i, j):
6-
nonlocal m, n
7-
if i == m:
8-
return j
9-
if j == 0 and grid[i][j] == -1:
10-
return -1
11-
if j == n - 1 and grid[i][j] == 1:
12-
return -1
13-
if grid[i][j] == 1 and grid[i][j + 1] == -1:
14-
return -1
15-
if grid[i][j] == -1 and grid[i][j - 1] == 1:
16-
return -1
17-
return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)
18-
19-
return [dfs(0, j) for j in range(n)]
1+
class Solution:
2+
def findBall(self, grid: List[List[int]]) -> List[int]:
3+
def dfs(i: int, j: int) -> int:
4+
if i == m:
5+
return j
6+
if j == 0 and grid[i][j] == -1:
7+
return -1
8+
if j == n - 1 and grid[i][j] == 1:
9+
return -1
10+
if grid[i][j] == 1 and grid[i][j + 1] == -1:
11+
return -1
12+
if grid[i][j] == -1 and grid[i][j - 1] == 1:
13+
return -1
14+
return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)
15+
16+
m, n = len(grid), len(grid[0])
17+
return [dfs(0, j) for j in range(n)]

0 commit comments

Comments
 (0)