There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at mostmaxMove moves to the ball.Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo10^9 + 7.
class Solution: # Time: O(max_move * m * n) # Space: O(m * n) def find_paths( self, m: int, n: int, max_move: int, start_row: int, start_column: int, ) -> int: mod = 1_000_000_007 # dp[r][c]: number of paths currently at cell (r, c) inside the grid. dp = [[0] * n for _ in range(m)] dp[start_row][start_column] = 1 paths = 0 for _ in range(max_move): nxt = [[0] * n for _ in range(m)] for row in range(m): for col in range(n): count = dp[row][col] if not count: continue for n_row, n_col in ( (row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1), ): if 0 <= n_row < m and 0 <= n_col < n: nxt[n_row][n_col] = (nxt[n_row][n_col] + count) % mod else: paths = (paths + count) % mod dp = nxt return paths