Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int countPaths(int[][] arr) {
- return paths(arr, 0, 0);
- }
- static int paths(int[][] grid, int row, int col) {
- int N = grid.length;
- int M = grid[0].length;
- if (row == N - 1 && col == M - 1) {
- return grid[row][col];
- }
- if (row == N - 1) {
- return grid[row][col] + paths(grid, row, col + 1);
- } else if (col == M - 1) {
- return grid[row][col] + paths(grid, row + 1, col);
- } else {
- return grid[row][col] + Math.max(paths(grid, row + 1, col), paths(grid, row, col + 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement