LeetCode Entry

1598. Crawler Log Folder

10.07.2024 easy 2024 kotlin rust

Filesystem path depth

1598. Crawler Log Folder easy blog post substack youtube 2024-07-10_07-40_1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/665

Problem TLDR

Filesystem path depth #easy

Intuition

Simulate the process and compute depth. Corner case: a path doesn’t move up from the root.

Approach

Let’s use fold iterator.

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code


    fun minOperations(logs: Array<String>) =
        logs.fold(0) { depth, op -> when (op) {
            "../" -> max(0, depth - 1)
            "./" -> depth else -> depth + 1 }}


    pub fn min_operations(logs: Vec<String>) -> i32 {
        logs.iter().fold(0, |depth, op| match op.as_str() {
            "../" => 0.max(depth - 1),
            "./" => depth,  _ =>  depth + 1 })
    }