码迷,mamicode.com
首页 > 其他好文 > 详细

动态规划(4)

时间:2020-04-05 09:49:46      阅读:44      评论:0      收藏:0      [点我收藏+]

标签:nbsp   com   获得   span   bsp   tin   lse   for   value   

技术图片

 

 

class Solution:
    def maxValue(self, grid: List[List[int]]) -> int:
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if i == 0 and j == 0:
                    continue
                elif i == 0:
                    grid[i][j] += grid[i][j-1]
                elif j == 0:
                    grid[i][j] += grid[i-1][j]
                else:
                    grid[i][j] += max(grid[i-1][j],grid[i][j-1])
        return grid[-1][-1]

 

class Solution:
    def maxValue(self, grid: List[List[int]]) -> int:
        if not grid or not grid[0]:
            return 0
        m, n = len(grid), len(grid[0])
        for i in range(1, n):
            grid[0][i] += grid[0][i-1]
        for i in range(1, m):
            grid[i][0] += grid[i-1][0]
            for j in range(1,n):
                grid[i][j] += max(grid[i-1][j], grid[i][j-1])
        return grid[-1][-1]

作者:luanz
链接:https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/solution/pythonchang-gui-dong-gui-jie-fa-jian-dan-yi-dong-b/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

动态规划(4)

标签:nbsp   com   获得   span   bsp   tin   lse   for   value   

原文地址:https://www.cnblogs.com/topass123/p/12635832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!