Posted on

Description

Submission

class Solution {
public:
    vector<int> findBall(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        vector<int> rets(n);
        for(int i = 0; i < n; ++i) {
            int col = i;
            int row = 0;
            
            for(; row < m; ++row) {
                if(grid[row][col] == -1) {
                    if(col - 1 < 0 || grid[row][col-1] == 1) {
                        rets[i] = -1;
                        break;
                    } else {
                        // safe to go left
                        col--;
                    }
                }
                
                if(grid[row][col] == 1) {
                    if(col + 1 >= n || grid[row][col+1] == -1) {
                        rets[i] = -1;
                        break;
                    } else {
                        // saft to go right 
                        col++;
                    }
                }
            }
            
            if(row == m) {
                rets[i] = col;
            }
        }
        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *