Crawler Log Folder
Let’s break down the solution to the problem step-by-step:
- Start in the main folder, initialize a variable
level
to keep track of the current folder level (0 means the main folder). - Iterate through the given logs:
- If the log is “../”, move to the parent folder if you are not already in the main folder.
- If the log is “./”, remain in the same folder.
- Otherwise, move to the child folder, i.e., increase the level by 1.
- Return the
level
, which indicates the minimum number of operations needed to go back to the main folder.
Here’s the code:
|
|
The code follows the problem’s requirements, iterates through the logs, and modifies the current folder level based on the log’s content. The time complexity of this code is O(n), where n is the number of logs.