Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 895 Bytes

388.md

File metadata and controls

36 lines (25 loc) · 895 Bytes

Longest Absolute File Path

Description

link


Solution

  • See Code

实际上这题是用一种类似于动态规划的方法来解决,只是递归方程非常简单,在递归的过程中得到最终的答案

lstrip即去除左边的所有需要去除的内容,splitlines,将所有\n分割的列取出来了


Code

Complexity T : O(n) M : O(n)

class Solution:
    def lengthLongestPath(self, input: str) -> int:
        maxlen = 0
        pathlen = {0: 0}
        for line in input.splitlines():
            name = line.lstrip('\t')
            depth = len(line) - len(name)
            if '.' in name:
                maxlen = max(maxlen, pathlen[depth] + len(name))
            else:
                pathlen[depth + 1] = pathlen[depth] + len(name) + 1
        return maxlen