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.
-
Initialization:
- We start by initializing a variable
depth
to0
to represent the main folder.
int depth = 0;
- We start by initializing a variable
-
Processing Each Log Entry:
- We loop through each string in the
logs
vector.
for (const string& log : logs) {
- We loop through each string in the
-
Handling
"../"
:- If the log entry is
"../"
, we move to the parent folder. Ifdepth
is greater than0
, we decrement it.
if (log == "../") { if (depth > 0) depth--;
- If the log entry is
-
Handling
"./"
:- If the log entry is
"./"
, we do nothing and stay in the current folder.
} else if (log != "./") {
- If the log entry is
-
Handling Other Folders:
- For any other folder move (e.g.,
"x/"
), we increment thedepth
.
depth++;
- For any other folder move (e.g.,
-
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;
- Finally, we return the
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;
}
};
-
Initialization:
- We start by initializing a variable
depth
to0
to represent the main folder.
int depth = 0;
- We start by initializing a variable
-
Processing Each Log Entry:
- We loop through each string in the
logs
array.
for (String log : logs) {
- We loop through each string in the
-
Handling
"../"
:- If the log entry is
"../"
, we move to the parent folder. Ifdepth
is greater than0
, we decrement it.
if (log.equals("../")) { if (depth > 0) depth--;
- If the log entry is
-
Handling
"./"
:- If the log entry is
"./"
, we do nothing and stay in the current folder.
} else if (!log.equals("./")) {
- If the log entry is
-
Handling Other Folders:
- For any other folder move (e.g.,
"x/"
), we increment thedepth
.
depth++;
- For any other folder move (e.g.,
-
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;
- Finally, we return the
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;
}
}
-
Initialization:
- We start by initializing a variable
depth
to0
to represent the main folder.
let depth = 0;
- We start by initializing a variable
-
Processing Each Log Entry:
- We loop through each string in the
logs
array.
for (const log of logs) {
- We loop through each string in the
-
Handling
"../"
:- If the log entry is
"../"
, we move to the parent folder. Ifdepth
is greater than0
, we decrement it.
if (log === "../") { if (depth > 0) depth--;
- If the log entry is
-
Handling
"./"
:- If the log entry is
"./"
, we do nothing and stay in the current folder.
} else if (log !== "./") {
- If the log entry is
-
Handling Other Folders:
- For any other folder move (e.g.,
"x/"
), we increment thedepth
.
depth++;
- For any other folder move (e.g.,
-
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;
- Finally, we return the
var minOperations = function(logs) {
let depth = 0;
for (const log of logs) {
if (log === "../") {
if (depth > 0) depth--;
} else if (log !== "./") {
depth++;
}
}
return depth;
};
-
Initialization:
- We start by initializing a variable
depth
to0
to represent the main folder.
depth = 0
- We start by initializing a variable
-
Processing Each Log Entry:
- We loop through each string in the
logs
list.
for log in logs:
- We loop through each string in the
-
Handling
"../"
:- If the log entry is
"../"
, we move to the parent folder. Ifdepth
is greater than0
, we decrement it.
if log == "../": if depth > 0: depth -= 1
- If the log entry is
-
Handling
"./"
:- If the log entry is
"./"
, we do nothing and stay in the current folder.
elif log != "./":
- If the log entry is
-
Handling Other Folders:
- For any other folder move (e.g.,
"x/"
), we increment thedepth
.
depth += 1
- For any other folder move (e.g.,
-
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
- Finally, we return the
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
-
Initialization:
- We start by initializing a variable
depth
to0
to represent the main folder.
depth := 0
- We start by initializing a variable
-
Processing Each Log Entry:
- We loop through each string in the
logs
slice.
for _, log := range logs {
- We loop through each string in the
-
Handling
"../"
:- If the log entry is
"../"
, we move to the parent folder. Ifdepth
is greater than0
, we decrement it.
if log == "../" { if depth > 0 { depth-- }
- If the log entry is
-
Handling
"./"
:- If the log entry is
"./"
, we do nothing and stay in the current folder.
} else if log != "./" {
- If the log entry is
-
Handling Other Folders:
- For any other folder move (e.g.,
"x/"
), we increment thedepth
.
depth++
- For any other folder move (e.g.,
-
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
- Finally, we return the
func minOperations(logs []string) int {
depth := 0
for _, log := range logs {
if log == "../" {
if depth > 0 {
depth--
}
} else if log != "./" {
depth++
}
}
return depth
}