classSolution: defmaxValue(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) dp = [[0] * n for i in grid] dp[0][0] = grid[0][0] for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] for i in range(1, n): dp[0][i] = dp[0][i-1] + grid[0][i] for i in range(1, m): for j in range(1, n): dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + grid[i][j] return dp[-1][-1]
classSolution: defmaxValue(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) for i in range(1, m): grid[i][0] += grid[i-1][0] for i in range(1, n): grid[0][i] += grid[0][i-1] for i in range(1, m): for j in range(1, n): grid[i][j] += max(grid[i][j-1], grid[i-1][j]) return grid[-1][-1]