1598. Crawler Log Folder - Explanation
Description
There is a file system that 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. (If you are already in the main folder, remain in the same folder)."./": Remain in the same folder."x/": Move to the child folder namedx(This folder is guaranteed to always exist).
You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.
The file system starts in the main folder, then the operations in logs are performed.
Return the minimum number of operations needed to go back to the main folder after the change folder operations.
Example 1:
Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2Explanation: After all the log operations, use this change folder operation "../" 2 times and go back to the main folder.
Example 2:
Input: logs = ["d1/","../","../","../"]
Output: 0Example 3:
Input: logs = ["d1/","d2/","../","d3/","../","d4/","../","d5/"]
Output: 2Constraints:
1 <= logs.length <= 10002 <= logs[i].length <= 10logs[i]contains lowercase English letters, digits,'.', and'/'.logs[i]follows the format described in the statement.- Folder names consist of lowercase English letters and digits.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Stack Data Structure - Understanding push/pop operations and how stacks model nested or hierarchical state
- String Comparison - Comparing strings for equality to distinguish between different folder operations
1. Stack
Intuition
A file system path can be naturally modeled using a stack. Moving into a folder pushes onto the stack, while moving to the parent folder pops from the stack. The operation "./" does nothing (stay in current folder). At the end, the stack's size represents how deep we are from the main folder, which equals the minimum operations needed to return.
Algorithm
- Initialize an empty stack.
- For each log operation:
- If it is
"../", pop from the stack if it is not empty (move to parent). - If it is
"./", do nothing (stay in current folder). - Otherwise, push the folder name onto the stack (move into child folder).
- If it is
- Return the size of the stack, representing the depth from the main folder.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Iteration
Intuition
We do not actually need to store the folder names since we only care about the depth. A simple counter can track how many levels deep we are. Moving into a folder increments the counter, moving to parent decrements it (but never below 0 since we cannot go above the main folder), and "./" leaves it unchanged.
Algorithm
- Initialize a depth counter to
0. - For each log operation:
- If it is
"./", skip (no change in depth). - If it is
"../", decrement the counter but ensure it does not go below0. - Otherwise, increment the counter (moving into a child folder).
- If it is
- Return the counter value as the minimum operations to return to main folder.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Allowing Depth to Go Negative
When processing "../", you must ensure the depth does not go below zero. Going above the main folder is impossible, so decrementing when already at depth 0 produces wrong results.
# Wrong: depth can become negative
if log == "../":
depth -= 1
# Correct: clamp at zero
if log == "../":
depth = max(0, depth - 1)Forgetting to Handle the Current Directory Operation
The operation "./" means stay in the current folder and should not change the depth. Treating it like a folder name and incrementing depth is a common mistake.
# Wrong: treating "./" as a folder
if log != "../":
depth += 1 # This incorrectly increments for "./"
# Correct: explicitly skip "./"
if log == "./":
continue
elif log == "../":
depth = max(0, depth - 1)
else:
depth += 1
Sign in to join the discussion