bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1301. Number of Paths with Max Score

1301. Number of Paths with Max Score

Leetcode Solutions

Dynamic Programming Approach

  1. Initialize a 2D array dp with tuples of (0, 0) for each cell in the board.
  2. Set dp[n-1][n-1] to (0, 1) as the starting point with a score of 0 and 1 path.
  3. Iterate over the board in reverse, starting from the bottom right corner.
  4. For each cell (i, j), if it is not an obstacle, calculate the maximum score from the adjacent cells that can be reached (down, right, and diagonally down-right).
  5. Update dp[i][j] with the new maximum score and the sum of the number of paths from the adjacent cells that lead to this maximum score, modulo 10^9 + 7.
  6. If the cell contains the character 'E', do not add its numeric value to the score.
  7. After filling the dp array, the answer will be in dp[0][0].
  8. If there are no paths to the end, return [0, 0].
UML Thumbnail

Depth-First Search with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...