-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball_fall.cpp
33 lines (28 loc) · 914 Bytes
/
ball_fall.cpp
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
class Solution {
public:
vector<int> findBall(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<int> v(n,0);
for(int i=0;i<n;i++){
int row=0, col=i; // current column
while(1){
int newcol = grid[row][col]+col;
if(0<=newcol && n>newcol && grid[row][col]==grid[row][newcol]){
row++;
col=newcol;
} else{
// There is a blocakage , break
v[i]=-1;
break;
}
if(row>=m){
// we reached out of the last row
v[i]=col;
break;
}
}
}
return v;
}
};