[Java/Python 3] Check the corresponding slope of its neighbor, w/ brief explanation and analysis..
Q & A\nQ1: Can you pls explain how you are finding the next cell. \nA1: I believe the problem description intentionally gives the hint: slopes of 1 and -1 for s
**Q & A**\nQ1: Can you pls explain how you are finding the next cell. \nA1: I believe the problem description intentionally gives the hint: slopes of `1` and `-1` for sliding to right and left. Therefore, adding the corresponding displacement, `slope * 1`, to current `column` will get the correct `column` of the next row; Also, the same slope of its neighbor is needed in order to avoid being stuck.\n\nQ2: Could you please explain more on the\n```\ngrid[r][x] != slope) { // Stuck on the the neighbor?\n```\npart?\nA2:  Ball will tend to fall right (slope = grid[r][x] = 1) or left (slope = grid[r][x] = -1), it need the corresponding neighbor\'s same tendency (or slope) to fall through, otherwise get stuck. \ne.g.,\ngrid[r][x] = 1, ball will tend to fall right and we need to check right neighbor\'s slope grid[r][x + 1]; If grid[r][x + 1] = 1, then the ball will fall right to the next row at (r + 1, x + 1), otherwise get stuck at (r, x).\n\nRewrite the code as following might be helpful to understand:\n```\n    public int[] findBall(int[][] grid) {\n        int n = grid[0].length;\n        int[] ans = new int[n];\n        outer:\n        for (int c = 0, m = grid.length; c < n; ++c) {\n            int x = c;\n            for (int r = 0; r < m; ++r) {\n                int xNeighbor = x + grid[r][x];\n                // Stuck on the left, right boundaries or on the neighbor? \n                if (xNeighbor < 0 || xNeighbor >= n || grid[r][x] != grid[r][xNeighbor]) {\n                    ans[c] = -1;\n                    continue outer;\n                }\n                x = xNeighbor;\n            }\n            ans[c] = x; \n        }\n        return ans;        \n    }\n```\n**End of Q & A**\n\nCheck if the corresponding slope of its neighbor is same: if yes, go to next row, otherwise stuck.\n```java\n    public int[] findBall(int[][] grid) {\n        int n = grid[0].length;\n        int[] ans = new int[n];\n        outer:\n        for (int c = 0, m = grid.length; c < n; ++c) {\n            int x = c;\n            for (int r = 0; r < m; ++r) {\n                int slope = grid[r][x];\n                x += slope;\n                if (x < 0 || x >= n || grid[r][x] != slope) { // Stuck on the left, right boundaries or on the neighbor?\n                    ans[c] = -1;\n                    continue outer;\n                }\n            }\n            ans[c] = x; \n        }\n        return ans;\n    }\n```\n```python\n    def findBall(self, grid: List[List[int]]) -> List[int]:\n        m, n = map(len, (grid, grid[0]))\n        ans = []\n        for c in range(n):\n            x = c\n            for r in range(m):\n                slope = grid[r][x]\n                x += grid[r][x]\n                if x < 0 or x >= n or grid[r][x] != slope:\n                    ans.append(-1)\n                    break    \n            else:        \n                ans.append(x)\n        return ans\n```\n\n**Analysis:**\n\nTime: O(m * n), space: O(n).
['Java', 'Python3']