python


  • 0
    class Solution(object):
        def minimumTotal(self, triangle):
            """
            :type triangle: List[List[int]]
            :rtype: int
            """
            if len(triangle) == 0:
                return 0
            path = [[triangle[0][0]]]
            for i in range(1, len(triangle)):
                temp = []
                for j in range(len(triangle[i])):
                    if j == 0:
                        temp.append(path[i - 1][0] + triangle[i][j])
                    elif j == len(triangle[i - 1]):
                        temp.append(path[i - 1][j - 1] + triangle[i][j])
                    else:
                        temp.append(min(path[i - 1][j - 1], path[i - 1][j]) + triangle[i][j])
                path.append(temp[:])
            return sorted(path[-1])[0]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.