# class Solution: # def maxValue(self, grid: List[List[int]]) -> int: # return self.maxCalc(grid,len(grid)-1,len(grid[0])-1) # def maxCalc(self, grid,i,j) -> int: # if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]): # return 0 # return max(self.maxCalc(grid,i-1,j),self.maxCalc(grid,i,j-1)) + grid[i][j] class Solution: def maxValue(self,grid:List[List[int]]) -> int: m,n = len(grid),len(grid[0]) ans = [[0 for i in range(n)] for j in range(m)] # print(len(ans[0][0])) ans[0][0]= grid[0][0] for i in range(1,m): ans[i][0] = grid[i][0] + ans[i-1][0] for j in range(1,n): ans[0][j] = grid[0][j] + ans[0][j-1] for i in range(1,m): for j in range(1,n): ans[i][j] = max(ans[i-1][j],ans[i][j-1])+grid[i][j] # print(ans) return ans[m-1][n-1]
dfs的背面就是dp
愿为天下目,萃聚六路华