Java | Simple while loop
The code is readable. A simple while loop is enough.\nNo need for DFS to pollute the call stack.\n\n\n\n\n\nclass Solution {\n    private int[][] grid;\n    pri
The code is readable. A simple while loop is enough.\nNo need for DFS to pollute the call stack.\n\n![image](https://assets.leetcode.com/users/images/1aa39ce6-d0d3-4a8c-a5ca-5a9464302dca_1667320373.7652721.png)\n\n\n```\nclass Solution {\n    private int[][] grid;\n    private int m;\n    private int n;\n\n    public int[] findBall(int[][] grid) {\n        this.grid = grid;\n        m = grid.length;\n        n = grid[0].length;\n\n        int[] res = new int[n];\n        for (int i = 0; i < n; i++) {\n            res[i] = calculateExitIndex(i);\n        }\n\n        return res;\n    }\n\n    public int calculateExitIndex(int startingColumn) {\n        int row = 0;\n        int column = startingColumn;\n\n        while (row < m) {\n            if (grid[row][column] == 1) {\n                if (column == n - 1 || grid[row][column + 1] == -1) return -1;\n                row++;\n                column++;\n                continue;\n            }\n\n            if (grid[row][column] == -1) {\n                if (column == 0 || grid[row][column - 1] == 1) return -1;\n                row++;\n                column--;\n                continue;\n            }\n        }\n\n        return column;\n    }\n}\n```
['Java']