Leetcode Problem 2684. Maximum Number of Moves in a Grid
2684. Maximum Number of Moves in a Grid
Leetcode Solutions
Dynamic Programming (DP) with Space Optimization
Initialize a 2D DP array with 2 rows and m columns, where m is the number of rows in the grid.
Iterate over the columns of the grid from the second-to-last column to the first column.
For each cell (row, col), calculate the maximum moves by checking the cells (row - 1, col + 1), (row, col + 1), and (row + 1, col + 1) in the next column.
Update the DP array with the maximum of the calculated moves if the value of the next cell is strictly greater than the current cell's value.
After processing all columns, find the maximum value in the first column of the DP array, which represents the maximum number of moves starting from the first column of the grid.