leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1706.cpp (753B)


      1 class Solution {
      2     int m, n;
      3 
      4     bool valid_column(int column) { return column >= 0 && column < n; }
      5 
      6     int simulate(int column, vector<vector<int>> &grid) {
      7         int row = 0;
      8         while (row < m) {
      9             int type = grid[row][column];
     10             int nextc = column + type;
     11             if (valid_column(nextc) && grid[row][nextc] == type) {
     12                 row++;
     13                 column = nextc;
     14             } else
     15                 return -1;
     16         }
     17         return column;
     18     }
     19 
     20   public:
     21     vector<int> findBall(vector<vector<int>> &grid) {
     22         m = grid.size();
     23         n = grid[0].size();
     24 
     25         vector<int> res;
     26         for (int i = 0; i < n; i++)
     27             res.push_back(simulate(i, grid));
     28         return res;
     29     }
     30 };