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 can collect, and the second is the number of such paths that you can take to get that maximum sum, taken modulo 10^9 + 7.
In case there is no path, return [0, 0].
Example 1:
Input: board = ["E23","2X2","12S"] Output: [7,1]
Example 2:
Input: board = ["E12","1X1","21S"] Output: [4,2]
Example 3:
Input: board = ["E11","XXX","11S"] Output: [0,0]
Constraints:
2 <= board.length == board[i].length <= 100In this approach, the idea is to use dynamic programming to keep track of the maximum score and the number of ways to achieve it, starting from the end point 'S' and working backwards to the start point 'E'. We will initialize a dp array where each cell will store two values: the maximum score to reach this cell and the number of ways to achieve that score. We iterate over each cell and update its value based on possible movements from the neighboring cells (up, left, and up-left) that are not blocked by an obstacle 'X'.
The solution uses a double array `dp` where `dp[i][j]` contains a tuple `(max_sum, path_count)` representing the maximum sum that can be collected to reach the cell `(i, j)` and the number of ways to reach that maximum sum. We start from 'S', marked initially with `(0, 1)`, meaning we start with sum 0 and 1 way. While iterating, we update cells with possible movements from adjacent cells. The time complexity is O(n^2), where n is the size of the board, and the space complexity is also O(n^2) due to the auxiliary array `dp`.
Java
Time Complexity: O(n^2) where n is the dimension of the square board.
Space Complexity: O(n^2) for the dp array.
Strategy: In this approach, we'll use dynamic programming to track the maximum path sum and the number of possible such paths from the start square 'S' to the end square 'E'. We'll initialize the bottom-right corner and populate upwards and leftwards.
We use a 2D array for max scores and paths. For each cell not blocked by 'X', the value of max score and path count is determined by considering up, left, and diagonal moves, ensuring to add the numeric value of the current cell to the path sum.
This C solution initializes a DP table where each cell represents the maximum score obtainable ending at that cell and how many ways it can be achieved. The program starts from 'S' and fills backwards to 'E', considering potential upward, leftward, and diagonal moves.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2), where n is the length of the board's edge.
Space Complexity: O(n^2), for the DP table.
Strategy: In this BFS approach, prioritize exploring paths with higher sums by deploying a max-heap structure, ensuring the most beneficial paths are expanded first. We'll backtrack from 'S' to 'E' while updating our path scores dynamically based on BFS execution order.
The C solution utilizes a BFS algorithm with a priority queue to determine all maximum possible paths. This ensures the highest sum paths are considered first, dynamically updating possible path scores using an adjacency-list-like mechanism, yet optimized using max-heaps for faster calculations.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2 log n), due to the priority queue.
Space Complexity: O(n^2).
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n^2) where n is the dimension of the square board. |
| Dynamic Programming from Bottom-Right to Top-Left | Time Complexity: O(n^2), where n is the length of the board's edge. |
| Breadth-First Search with Priority Queue | Time Complexity: O(n^2 log n), due to the priority queue. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,166 views views
Watch 9 more video solutions →Practice Number of Paths with Max Score with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor