Number of Paths with Max Score - Complete Solution Guide
Number of Paths with Max Score is LeetCode problem 1301, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character 'S' . You need to reach the top left square marked with the character 'E' . The rest of the squares are labeled either with a numeric character 1, 2, ..., 9 or with an obstacle 'X' . In one move you can go up, left or up-left (diagonally) only if there is no obstacle there. Return a list of two integers: the first integer is the maximum sum of numeric characters you
Detailed Explanation
The problem asks us to find the maximum possible score and the number of paths to achieve that score on a square board. We start from the bottom-right cell ('S') and need to reach the top-left cell ('E'). We can only move up, left, or diagonally up-left. The board contains numbers (1-9), obstacles ('X'), the start ('S'), and the end ('E'). The score is the sum of the numbers encountered along the path, and we need to return the maximum score and the number of paths that achieve this maximum score modulo 10^9 + 7. If no path exists, we return [0, 0].
Solution Approach
The solution uses dynamic programming to find the maximum score and the number of paths. A 3D DP array `dp[i][j][2]` is used to store the maximum score and the number of paths to reach cell (i, j). The algorithm iterates backwards from the destination cell ('S') to the source cell ('E'). For each cell (i, j), it considers the possible moves from the adjacent cells (i+1, j), (i, j+1), and (i+1, j+1). It calculates the maximum score and the number of paths based on the scores and paths from these adjacent cells. The result is stored in `dp[i][j][0]` and `dp[i][j][1]`, respectively.
Step-by-Step Algorithm
- Step 1: Initialize the DP array `dp[n+1][n+1][2]` with -1 for the score and 0 for the number of paths. Set `dp[n-1][n-1][0] = 0` and `dp[n-1][n-1][1] = 1` to represent the starting position with a score of 0 and one path.
- Step 2: Iterate through the board backwards, from row `n-1` to 0 and column `n-1` to 0.
- Step 3: If the current cell `board[i][j]` is an obstacle ('X'), skip to the next iteration.
- Step 4: For each cell (i, j), consider the possible moves: (i+1, j), (i, j+1), and (i+1, j+1). These are the cells we could have come FROM to reach (i, j).
- Step 5: For each valid move, check if the score of the adjacent cell is not -1 (meaning it is reachable).
- Step 6: If the score of the adjacent cell is greater than the current maximum score for cell (i, j), update the maximum score and the number of paths for cell (i, j) with the adjacent cell's score and number of paths. If the score of the adjacent cell is equal to the current maximum score, add the adjacent cell's number of paths to the current number of paths (modulo 10^9 + 7).
- Step 7: If a path to cell (i, j) is found (maximum score is not -1), add the value of the current cell (if it's a digit) to the maximum score and store the updated score and number of paths in `dp[i][j][0]` and `dp[i][j][1]`.
- Step 8: After the iteration is complete, the result is stored in `dp[0][0][0]` (maximum score) and `dp[0][0][1]` (number of paths).
- Step 9: If the maximum score is -1 (meaning no path exists), return [0, 0]. Otherwise, return the maximum score and the number of paths.
Key Insights
- Insight 1: Dynamic Programming is essential because we need to explore multiple paths and memoize intermediate results (maximum score and number of paths) to avoid redundant calculations.
- Insight 2: We iterate backwards from the destination ('S') to the source ('E') using dynamic programming. This simplifies the logic for calculating maximum score and number of paths since we build up the solution from a known base case.
- Insight 3: Handle obstacles ('X') by not processing those cells in the DP iteration. If a cell has an obstacle, it cannot contribute to any path.
- Insight 4: Using a 3D DP array dp[i][j][2] allows storing both the maximum score and number of paths for each cell (i, j).
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: Samsung.