Crawler Log Folder — LeetCode 1598 Python Solution
- Problem
- #1598
- Pattern
- Stack
- Reading time
- 2 min
- Source
- leetcode.com
The problem
The Leetcode file system keeps a log each time some user performs a change folder operation. The operations are described below: "../" : Move to the parent folder of the current folder.
Example
- Input
- logs = ["d1/","d2/","../","d21/","./"]
- Output
- 2
- Explanation
- Use this change folder operation "../" 2 times and go back to the main folder.
Python solution
class Solution:
def minOperations(self, logs: List[str]) -> int:
ans = 0
for v in logs:
if v == "../":
ans = max(0, ans - 1)
elif v[0] != ".":
ans += 1
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Stack
When the most recent unresolved thing is the one that matters, use a stack. LeetCode 1598. Crawler Log Folder is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Stack.
The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1598. Crawler Log Folder?
- LeetCode 1598. Crawler Log Folder is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1598. Crawler Log Folder?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1598. Crawler Log Folder?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 1598. Crawler Log Folder cover?
- LeetCode 1598. Crawler Log Folder is tagged Stack, Array and String on LeetCode.