Skip to content

2684. Maximum Number of Moves in a Grid #763

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use Dynamic Programming (DP) to keep track of the maximum number of moves from each cell, starting from any cell in the first column. Here’s the step-by-step approach:

Approach:

  1. Define DP Array: Let dp[row][col] represent the maximum number of moves possible starting from grid[row][col]. Initialize this with 0 for all cells.

  2. Traverse the Grid:

    • Start from the last column and move backward to the first column. For each cell in column col, calculate possible moves for col-1.
    • Update dp[row][col] based on possible moves (row - 1, col + 1), (row, col + 1), and (row + 1, col + 1), only if the value of the destination cell is strictly greater than the current cell.
  3. Calculate the Ma…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Oct 29, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Oct 29, 2024
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants