Python with Stack


  • 0

    Not as cool as this one but thought I'd post anyways.

    class Solution(object):
        def lengthLongestPath(self, input):
            stack,mx = collections.deque(),0
            for line in input.splitlines():
                tabCount = line.count('\t')
                for _ in xrange(len(stack)-tabCount):
                    stack.pop()
                length = (stack[-1]+1 if len(stack) > 0 else 0) + len(line) - tabCount
                if '.' in line:
                    mx = max(mx,length)
                else:
                    stack.append(length)
            return mx

Log in to reply
 

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