forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
70 lines (59 loc) · 1.67 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// Source : https://leetcode.com/problems/where-will-the-ball-fall/
/// Author : liuyubobobo
/// Time : 2020-12-29
#include <iostream>
#include <vector>
using namespace std;
/// Simulation
/// Time Complexity: O(m * n)
/// Space Complexity: O(1)
class Solution {
public:
vector<int> findBall(vector<vector<int>>& grid) {
int R = grid.size(), C = grid[0].size();
vector<int> res(C, -1);
for(int j = 0; j < C; j ++){
int curx = 0, cury = j;
while(curx < R){
if(grid[curx][cury] == 1){
if(cury + 1 >= C || grid[curx][cury + 1] == -1)
break;
else{
assert(grid[curx][cury + 1] == 1);
curx ++, cury ++;
}
}
else{ // grid[curx][cury] == -1
if(cury - 1 < 0 || grid[curx][cury - 1] == 1)
break;
else{
assert(grid[curx][cury - 1] == -1);
curx ++, cury --;
}
}
if(curx == R) res[j] = cury;
}
}
return res;
}
};
void print_vec(const vector<int>& v){
for(int e: v) cout << e << " "; cout << endl;
}
int main() {
vector<vector<int>> grid1 = {
{1,1,1,-1,-1},
{1,1,1,-1,-1},
{-1,-1,-1,1,1},
{1,1,1,1,-1},
{-1,-1,-1,-1,-1}
};
print_vec(Solution().findBall(grid1));
// 1 -1 -1 -1 -1
vector<vector<int>> grid2 = {
{-1}
};
print_vec(Solution().findBall(grid2));
// -1
return 0;
}