Skip to content

Latest commit

 

History

History
345 lines (257 loc) · 7.2 KB

File metadata and controls

345 lines (257 loc) · 7.2 KB

Crawler Log Folder Solutions

This repository contains solutions to the "Crawler Log Folder" problem from LeetCode in multiple programming languages. Each solution is explained step-by-step to help you understand the logic and implementation.

Table of Contents


C++ Solution

Step-by-Step Explanation

  1. Initialization:

    • We start by initializing a variable depth to 0 to represent the main folder.
    int depth = 0;
  2. Processing Each Log Entry:

    • We loop through each string in the logs vector.
    for (const string& log : logs) {
  3. Handling "../":

    • If the log entry is "../", we move to the parent folder. If depth is greater than 0, we decrement it.
    if (log == "../") {
        if (depth > 0) depth--;
  4. Handling "./":

    • If the log entry is "./", we do nothing and stay in the current folder.
    } else if (log != "./") {
  5. Handling Other Folders:

    • For any other folder move (e.g., "x/"), we increment the depth.
    depth++;
  6. Returning the Result:

    • Finally, we return the depth, which represents the minimum number of operations needed to go back to the main folder.
    return depth;

Full C++ Code

class Solution {
public:
    int minOperations(vector<string>& logs) {
        int depth = 0;
        for (const string& log : logs) {
            if (log == "../") {
                if (depth > 0) depth--;
            } else if (log != "./") {
                depth++;
            }
        }
        return depth;
    }
};

Java Solution

Step-by-Step Explanation

  1. Initialization:

    • We start by initializing a variable depth to 0 to represent the main folder.
    int depth = 0;
  2. Processing Each Log Entry:

    • We loop through each string in the logs array.
    for (String log : logs) {
  3. Handling "../":

    • If the log entry is "../", we move to the parent folder. If depth is greater than 0, we decrement it.
    if (log.equals("../")) {
        if (depth > 0) depth--;
  4. Handling "./":

    • If the log entry is "./", we do nothing and stay in the current folder.
    } else if (!log.equals("./")) {
  5. Handling Other Folders:

    • For any other folder move (e.g., "x/"), we increment the depth.
    depth++;
  6. Returning the Result:

    • Finally, we return the depth, which represents the minimum number of operations needed to go back to the main folder.
    return depth;

Full Java Code

class Solution {
    public int minOperations(String[] logs) {
        int depth = 0;
        for (String log : logs) {
            if (log.equals("../")) {
                if (depth > 0) depth--;
            } else if (!log.equals("./")) {
                depth++;
            }
        }
        return depth;
    }
}

JavaScript Solution

Step-by-Step Explanation

  1. Initialization:

    • We start by initializing a variable depth to 0 to represent the main folder.
    let depth = 0;
  2. Processing Each Log Entry:

    • We loop through each string in the logs array.
    for (const log of logs) {
  3. Handling "../":

    • If the log entry is "../", we move to the parent folder. If depth is greater than 0, we decrement it.
    if (log === "../") {
        if (depth > 0) depth--;
  4. Handling "./":

    • If the log entry is "./", we do nothing and stay in the current folder.
    } else if (log !== "./") {
  5. Handling Other Folders:

    • For any other folder move (e.g., "x/"), we increment the depth.
    depth++;
  6. Returning the Result:

    • Finally, we return the depth, which represents the minimum number of operations needed to go back to the main folder.
    return depth;

Full JavaScript Code

var minOperations = function(logs) {
    let depth = 0;
    for (const log of logs) {
        if (log === "../") {
            if (depth > 0) depth--;
        } else if (log !== "./") {
            depth++;
        }
    }
    return depth;
};

Python Solution

Step-by-Step Explanation

  1. Initialization:

    • We start by initializing a variable depth to 0 to represent the main folder.
    depth = 0
  2. Processing Each Log Entry:

    • We loop through each string in the logs list.
    for log in logs:
  3. Handling "../":

    • If the log entry is "../", we move to the parent folder. If depth is greater than 0, we decrement it.
    if log == "../":
        if depth > 0:
            depth -= 1
  4. Handling "./":

    • If the log entry is "./", we do nothing and stay in the current folder.
    elif log != "./":
  5. Handling Other Folders:

    • For any other folder move (e.g., "x/"), we increment the depth.
    depth += 1
  6. Returning the Result:

    • Finally, we return the depth, which represents the minimum number of operations needed to go back to the main folder.
    return depth

Full Python Code

class Solution:
    def minOperations(self, logs: List[str]) -> int:
        depth = 0
        for log in logs:
            if log == "../":
                if depth > 0:
                    depth -= 1
            elif log != "./":
                depth += 1
        return depth

Go Solution

Step-by-Step Explanation

  1. Initialization:

    • We start by initializing a variable depth to 0 to represent the main folder.
    depth := 0
  2. Processing Each Log Entry:

    • We loop through each string in the logs slice.
    for _, log := range logs {
  3. Handling "../":

    • If the log entry is "../", we move to the parent folder. If depth is greater than 0, we decrement it.
    if log == "../" {
        if depth > 0 {
            depth--
        }
  4. Handling "./":

    • If the log entry is "./", we do nothing and stay in the current folder.
    } else if log != "./" {
  5. Handling Other Folders:

    • For any other folder move (e.g., "x/"), we increment the depth.
    depth++
  6. Returning the Result:

    • Finally, we return the depth, which represents the minimum number of operations needed to go back to the main folder.
    return depth

Full Go Code

func minOperations(logs []string) int {
    depth := 0
    for _, log := range logs {
        if log == "../" {
            if depth > 0 {
                depth--
            }
        } else if log != "./" {
            depth++
        }
    }
    return depth
}