Skip to content

Commit

Permalink
Time: 3 ms (100%), Space: 54.5 MB (100%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 29, 2024
1 parent 06647ea commit 63c06c8
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
int max = 0;
public int maxMoves(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
getMoves(grid, i, 0, 0, 0);
}
return max;
}

private void getMoves(int[][] grid, int i, int j, int steps, int prev) {
if (i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] > 0 && Math.abs(grid[i][j]) > Math.abs(prev)) {
grid[i][j] = -grid[i][j];
max = Math.max(max, steps);
getMoves(grid, i - 1, j + 1, steps + 1, grid[i][j]);
getMoves(grid, i, j + 1, steps + 1, grid[i][j]);
getMoves(grid, i + 1, j + 1, steps + 1, grid[i][j]);
}
}
}

0 comments on commit 63c06c8

Please sign in to comment.