1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

java: add “2684. Maximum Number of Moves in a Grid”

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-01-03 19:04:35 +01:00
parent b229608723
commit 6d44469c3c
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,27 @@
class Solution {
public int maxMoves(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
for (int x = grid[0].length - 2; x >= 0; --x) {
for (int y = 0; y < dp.length; ++y) {
// check neighbours for moves
for (int dy = -1; dy <= 1; ++dy) {
int ny = y + dy;
if (ny < 0 || ny >= dp.length || grid[y][x] >= grid[ny][x + 1]) {
continue;
}
dp[y][x] = Math.max(dp[y][x], 1 + dp[ny][x + 1]);
}
}
}
int m = 0;
for (int y = 0; y < grid.length; ++y) {
m = Math.max(m, dp[y][0]);
}
return m;
}
}