-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path388_longest_absolute_file_path.py
More file actions
50 lines (38 loc) · 992 Bytes
/
388_longest_absolute_file_path.py
File metadata and controls
50 lines (38 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Main Concept:
if meet file, record the maximum size of the abs path `root/dir/file`
if meet dir, save current size for `root/dir`
for files:
two cases are at same depth
1. for hidden files
2. for extension in file
for dirs:
do NOT save the max size in each depth
since the children in same depth
may be under different parent
- root
- p1
- f-loooooong-1
- p2
- f-short-2
there is error in that case if save the max size
"""
class Solution:
def lengthLongestPath(self, path):
"""
:type path: str
:rtype: int
"""
ans = 0
if not path:
return ans
dep2size = {0: 0}
for line in path.split('\n'):
name = line.lstrip('\t')
size = len(name)
depth = len(line) - len(name)
if '.' in name:
ans = max(ans, dep2size[depth] + size)
else:
dep2size[depth + 1] = dep2size[depth] + size + 1
return ans