easy +10 pts

Crawler Log Folder

Simulate a file explorer's folder navigation using a stack-like log of operations.

The LeetCode file system keeps a log of user actions: "./" stays in the current folder, "../" moves to the parent folder (if already at the main folder, it stays), "x/" moves into a child folder named x. Given a list of strings log where log[i] is one of these three types, write a function min_operations(log) that returns the minimum number of operations needed to go back to the main folder after executing all operations. The main folder is the root level (depth 0), and each "x/" operation increases depth by 1, "../" decreases depth by 1 (but not below 0), and "./" does nothing.

Constraints

1 <= len(log) <= 1000; each log[i] is one of "x/", "../", or "./", where x consists of lowercase English letters and digits, possibly with hyphens. Return an integer.

Example

['>>> min_operations(["d1/","d2/","../","d21/","./"])', '2', '>>> min_operations(["d1/","d2/","./","d3/","../","d31/"])', '3', '>>> min_operations(["../","../","./"])', '0', '>>> min_operations(["a/","b/","../","c/","./","../"])', '1']
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Track the current depth as an integer starting at 0.
For "x/", increment depth by 1; for "../", decrement depth by 1 but never below 0; for "./", do nothing.
The answer is the final depth, because each move up or down changes depth by exactly 1, and moving to a sibling requires going up and then down, so depth is the minimum operations to return to root.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.