[Java/C++/Python] Solution with Explanation
Explanation\nWe drop the ball at grid[0][i]\nand track ball position i1, which initlized as i.\n\nAn observation is that,\nif the ball wants to move from i1 to 
# **Explanation**\nWe drop the ball at `grid[0][i]`\nand track ball position `i1`, which initlized as `i`.\n\nAn observation is that,\nif the ball wants to move from `i1` to `i2`,\nwe must have the board direction `grid[j][i1] == grid[j][i2]`\n<br>\n\n# **Complexity**\nTime `O(mn)`\nSpace `O(n)`\n<br>\n\n**Java:**\n```java\n    public int[] findBall(int[][] grid) {\n        int m = grid.length, n = grid[0].length, res[] = new int[n];\n        for (int i = 0; i < n; ++i) {\n            int i1 = i, i2;\n            for (int j = 0; j < m; ++j) {\n                i2 = i1 + grid[j][i1];\n                if (i2 < 0 || i2 >= n || grid[j][i2] != grid[j][i1]) {\n                    i1 = -1;\n                    break;\n                }\n                i1 = i2;\n            }\n            res[i] = i1;\n        }\n        return res;\n    }\n```\n\n**C++:**\n```cpp\n    vector<int> findBall(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        vector<int> res;\n        for (int i = 0; i < n; ++i) {\n            int i1 = i, i2;\n            for (int j = 0; j < m; ++j) {\n                i2 = i1 + grid[j][i1];\n                if (i2 < 0 || i2 >= n || grid[j][i2] != grid[j][i1]) {\n                    i1 = -1;\n                    break;\n                }\n                i1 = i2;\n            }\n            res.push_back(i1);\n        }\n        return res;\n    }\n```\n\n**Python:**\n```py\n    def findBall(self, grid):\n        m, n = len(grid), len(grid[0])\n\n        def test(i):\n            for j in xrange(m):\n                i2 = i + grid[j][i]\n                if i2 < 0 or i2 >= n or grid[j][i2] != grid[j][i]:\n                    return -1\n                i = i2\n            return i\n        return map(test, range(n))\n```\n
['C', 'Python', 'Java']