Solving Leetcode Interviews in Seconds with AI: Number of Paths with Max Score
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1301" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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 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 <= 100
Explanation
Here's the breakdown of the solution:
- Dynamic Programming: Use a 2D DP table to store the maximum sum achievable and the number of paths to achieve that sum for each cell. Start from the end ('S') and work our way backward to the start ('E').
- Iterate Backwards: Iterate through the board from the bottom-right to the top-left. For each cell, calculate the maximum sum and the number of paths based on the maximum sums and paths from the cells it can reach (up, left, and up-left).
Handle Obstacles: If a cell is an obstacle ('X'), both the sum and the path count are set to 0.
Runtime Complexity: O(N^2) where N is the length of the board. Storage Complexity: O(N^2).
Code
def paths_with_max_score(board):
"""
Finds the maximum sum of numeric characters and the number of paths to get that sum.
Args:
board: A list of strings representing the board.
Returns:
A list of two integers: the maximum sum and the number of paths.
"""
n = len(board)
dp = [[(0, 0) for _ in range(n)] for _ in range(n)] # (max_sum, path_count)
mod = 10**9 + 7
# Initialize the starting cell 'S'
dp[n - 1][n - 1] = (0, 1)
# Iterate backwards through the board
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
if board[i][j] == 'X':
continue
if i == n - 1 and j == n - 1:
continue
max_sum = -1
path_count = 0
# Check the cell below (i+1, j)
if i + 1 < n and board[i + 1][j] != 'X':
curr_sum, curr_paths = dp[i + 1][j]
if curr_sum > -1:
if curr_sum > max_sum:
max_sum = curr_sum
path_count = curr_paths
elif curr_sum == max_sum:
path_count = (path_count + curr_paths) % mod
# Check the cell to the right (i, j+1)
if j + 1 < n and board[i][j + 1] != 'X':
curr_sum, curr_paths = dp[i][j + 1]
if curr_sum > -1:
if curr_sum > max_sum:
max_sum = curr_sum
path_count = curr_paths
elif curr_sum == max_sum:
path_count = (path_count + curr_paths) % mod
# Check the cell diagonally down-right (i+1, j+1)
if i + 1 < n and j + 1 < n and board[i + 1][j + 1] != 'X':
curr_sum, curr_paths = dp[i + 1][j + 1]
if curr_sum > -1:
if curr_sum > max_sum:
max_sum = curr_sum
path_count = curr_paths
elif curr_sum == max_sum:
path_count = (path_count + curr_paths) % mod
if max_sum > -1:
val = 0
if board[i][j] != 'E' and board[i][j] != 'S':
val = int(board[i][j])
dp[i][j] = (max_sum + val, path_count)
return list(dp[0][0])