[Python/C++] let the balls fall row by row - O(MN)
Idea\n\nWe simply let the balls fall row by row. At each row, we check if each ball gets stuck or not.\n\n\n\n\nComplexity\n\n- Time complexity: O(MN)\n- Space 
**Idea**\n\nWe simply let the balls fall row by row. At each row, we check if each ball gets stuck or not.\n\n\n</br>\n\n**Complexity**\n\n- Time complexity: `O(MN)`\n- Space complexity: `O(N)`, or `O(1)` extra\n\n</br>\n\n**Python**\n\n```Python\nclass Solution:\n    def findBall(self, grid: List[List[int]]) -> List[int]:\n        m, n = len(grid), len(grid[0])\n        ans = list(range(n))\n        \n        for r in range(m):\n            for i in range(n):\n                c = ans[i]\n                if c == -1: continue\n                # check stuck\n                nxt_c = c + grid[r][c]\n                if nxt_c < 0 or nxt_c >= n or grid[r][nxt_c] == -grid[r][c]:\n                    ans[i] = -1\n                    continue\n                ans[i] += grid[r][c]\n        \n        return ans\n```\n\n</br>\n\n**C++**\n\n```C++\nclass Solution {\npublic:\n    vector<int> findBall(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        vector<int> ans(n);\n        iota(ans.begin(), ans.end(), 0);\n        \n        for (int r = 0; r < m; ++r) {\n            for (int i = 0; i < n; ++i) {\n                int c = ans[i];\n                if (c == -1) continue;\n                // check stuck\n                int nxt_c = c + grid[r][c];\n                if (nxt_c < 0 || nxt_c >= n || grid[r][nxt_c] == -grid[r][c]) {\n                    ans[i] = -1;\n                    continue;\n                }\n                ans[i] = ans[i] + grid[r][c];\n            }\n        }\n        \n        return ans;\n    }\n};\n```
[]