Python bottom-up algorithm with syntax sugar


  • 0
    C

    This code was inspired by @caikehe's Python bottom up O(n) space algorithm in here, but used Python's syntax sugars to eliminate reversed indexing.

    class Solution(object):
        def minimumTotal(self, triangle):
            min_path_bottom_up = triangle.pop()
            for row in reversed(triangle):
                for index, element in enumerate(row):
                    min_path_bottom_up[index] = element + min(
                        min_path_bottom_up[index], 
                        min_path_bottom_up[index + 1])
    
            return min_path_bottom_up[0]
    

Log in to reply
 

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