NeetCode #343LC-1598EasyStack
← Back to All Problems

#343 · #1598 · Crawler Log Folder(文件夹操作日志搜集器)

📌 Problem Statement & Constraints

You are given a list of folder operations: "../" moves to the parent folder (a no-op at the root), "./" stays in the current folder, and "x/" moves into a child folder. Return the minimum number of operations to return to the main folder. Constraints: 1 <= logs.length <= 10^3.

💡 Core Algorithmic Approaches

  1. Track the current depth as an integer.
  2. ../ decrements the depth, clamped at 0 (you cannot go above the root).
  3. ./ does nothing; any other operation increments the depth.
  4. The answer is the final depth, since returning to the root takes exactly one ../ per level.

💻 Benchmark Python3 Implementation

class Solution:
    def minOperations(self, logs: List[str]) -> int:
        depth = 0
        for op in logs:
            if op == "../":
                depth = max(0, depth - 1)  # cannot go above the root
            elif op == "./":
                continue                   # no movement
            else:
                depth += 1                 # enter a child folder
        return depth

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n): one pass.
💾 Space Complexity
O(1): a single counter.

⚠️ Interview Pitfalls & Follow-ups

  • Decrementing below 0: max(0, depth - 1) is required; otherwise the depth could go negative and the answer would be wrong.
  • Treating ./ as a child folder: it is a no-op.
  • Using a stack: unnecessary, since only the depth matters -- no path reconstruction is needed.
  • Matching with startswith('..'): the operation strings are exact, so equality is cleaner.