Problem: 2684. 矩阵中移动的最大次数
复杂度
⏰ 时间复杂度: O ( n m ) O(nm) O(nm)
🌎 空间复杂度: O ( n m ) O(nm) O(nm)
Code
class Solution {
public int maxMoves(int[][] grid){int n = grid.length;int m = grid[0].length;int[][] f = new int[n][m];int max = -1;for (int j = 1; j < m; j++){for (int i = 0; i < n; i++){if (grid[i][j] > grid[i][j - 1] && f[i][j - 1] == j - 1)f[i][j] = f[i][j - 1] + 1;if (i - 1 >= 0 && grid[i][j] > grid[i - 1][j - 1] && f[i - 1][j - 1] == j - 1)f[i][j] = Math.max(f[i][j], f[i - 1][j - 1] + 1);if (i + 1 < n && grid[i][j] > grid[i + 1][j - 1] && f[i + 1][j - 1] == j - 1)f[i][j] = Math.max(f[i][j], f[i + 1][j - 1] + 1);max = max > f[i][j] ? max : f[i][j];}}return max;}
}