diff --git a/algorithms/cpp/WhereWilltheBallFall/WhereWilltheBallFall.cpp b/algorithms/cpp/WhereWilltheBallFall/WhereWilltheBallFall.cpp new file mode 100644 index 00000000..1f920b16 --- /dev/null +++ b/algorithms/cpp/WhereWilltheBallFall/WhereWilltheBallFall.cpp @@ -0,0 +1,32 @@ +// https://leetcode.com/problems/where-will-the-ball-fall/ + +class Solution { +public: + vector findBall(vector>& grid) { + int N = grid.size(); + int M = grid[0].size(); + vector res; + + for(int ball=0;ball= M || j < 0) + break; + // if adjacent cell is closed (eg. going left and current is right) + if(cell != grid[i][j]) + break; + // go down one cell + i++; + } + // if got out safely. push column num ( j ) else push ( -1 ) + i == N ? res.push_back(j) : res.push_back(-1); + } + return res; + } +}; +