Java Easy To understand DFS
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n// class Solution {\n//     public int[] findBall(int[][] grid) {\n//         int n=grid.length;\n//         int m=grid[0].length;\n//         int[] ans = new int[n];\n//         for(int j=0; j<m; j++){\n//             int cpos=j;\n//             int npos=-1;\n\n//             for(int i=0; i<n; i++){\n//                 npos=cpos+grid[i][cpos];  //1->right, -1->left\n\n//                 if(npos<0 || npos>=m || grid[i][cpos]!=grid[i][npos]){\n//                     cpos=-1;\n//                     break;\n//                 }\n//                 cpos=npos;\n//             }\n//             ans[j]=cpos;\n//         }\n//         return ans;\n//     }\n// }\n\nclass Solution {\n    public int[] findBall(int[][] grid) {\n\n        int[] result = new int[grid[0].length];\n\n        for(int i=0; i<result.length; i++){\n            result[i] = dfs(0,i,grid);\n        }\n        return result;\n    }\n\n    private int dfs(int i, int j, int[][] grid){\n\n        if(i == grid.length){\n            return j;\n        }\n\n        int newj = j + grid[i][j];\n\n        if(newj<0 || newj>=grid[0].length || grid[i][j] != grid[i][newj]){\n            return -1;\n        }\n\n        \n\n       \n        return dfs(i+1,newj,grid);   \n       \n\n    }\n}\n```
['Depth-First Search', 'Java']