-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1706.cpp
More file actions
30 lines (26 loc) · 753 Bytes
/
Copy path1706.cpp
File metadata and controls
30 lines (26 loc) · 753 Bytes
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
class Solution {
int m, n;
bool valid_column(int column) { return column >= 0 && column < n; }
int simulate(int column, vector<vector<int>> &grid) {
int row = 0;
while (row < m) {
int type = grid[row][column];
int nextc = column + type;
if (valid_column(nextc) && grid[row][nextc] == type) {
row++;
column = nextc;
} else
return -1;
}
return column;
}
public:
vector<int> findBall(vector<vector<int>> &grid) {
m = grid.size();
n = grid[0].size();
vector<int> res;
for (int i = 0; i < n; i++)
res.push_back(simulate(i, grid));
return res;
}
};