Skip to content

Commit 7825ce2

Browse files
committed
388_Longest_Absolute_File_Path
1 parent bdaec07 commit 7825ce2

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@
8585
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
8686
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
8787
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | |
88+
| 388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/388_Longest_Absolute_File_Path.py) | 1. Store last length and rindex, O(n) and O(n) |
8889

8990

9091
| # | To Understand |
9192
|---| ----- |
9293
| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) |
93-
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥ |
94+
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥
9495

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def lengthLongestPath(self, input):
3+
"""
4+
:type input: str
5+
:rtype: int
6+
"""
7+
if input is None or len(input) == 0:
8+
return 0
9+
lines = input.split('\n')
10+
last_level_len = [0] * (len(lines) + 1); max_len = 0
11+
for line in lines:
12+
try:
13+
level = line.rindex('\t') + 1
14+
except:
15+
level = 0
16+
cur_len = last_level_len[level + 1] = last_level_len[level] + len(line) - level + 1
17+
if '.' in line:
18+
max_len = max(max_len, cur_len - 1)
19+
return max_len
20+
21+
if __name__ == '__main__':
22+
s = Solution()
23+
print s.lengthLongestPath("dir\n file.txt")
24+

0 commit comments

Comments
 (0)