剑指offer47

# 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

上一篇:LeetCode.最小路径和(动态规划,状态压缩)


下一篇:CSS Grid布局(一)