O(n) space clean python code using queue


  • 0
    class Solution(object):
        def minimumTotal(self, triangle):
            queue, MAX = collections.deque(), float('inf')
    
            for row in triangle:
                pre = MAX
    
                for i in xrange(len(queue)):
                    queue.append(min(pre, queue[0]) + row[i])
                    pre = queue.popleft()
    
                queue.append([pre, 0][pre == MAX] + row[-1])
    
            return min(queue)

Log in to reply
 

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