欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DP-LeetCode1143. 最长公共子序列(Python)

程序员文章站 2022-03-24 20:48:57
...

1、题目描述

DP-LeetCode1143. 最长公共子序列(Python)

2、代码详解

自下向上

class Solution(object):
    # Modify the original triangle, bottom-up
    def minimumTotal(self, triangle):
        """
        :type triangle: List[List[int]]
        :rtype: int
        """
        if not triangle:
            return
        for i in range(len(triangle) - 2, -1, -1):
            for j in range(len(triangle[i])):
                triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1])
        return triangle[0][0]

    # bottom-up, O(n) space
    def minimumTotal2(self, triangle):
        if not triangle:
            return
        res = triangle[-1]  # 转一维res = list(triangle[-1])
        for i in range(len(triangle) - 2, -1, -1):
            for j in range(len(triangle[i])):
                res[j] = min(res[j], res[j + 1]) + triangle[i][j]
        return res[0]
s = Solution()
triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
print(s.minimumTotal(triangle))
triangle2 = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
print(s.minimumTotal2(triangle2))

 

相关标签: 动态规划