[LeetCode] Crawler Log Folder

1598. Crawler Log Folder

The Leetcode file system keeps a log each time some user performs a change folder operation.

The operations are described below:

  • "../" : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
  • "./" : Remain in the same folder.
  • "x/" : Move to the child folder named x (This folder is guaranteed to always exist).

You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.

The file system starts in the main folder, then the operations in logs are performed.

Return the minimum number of operations needed to go back to the main folder after the change folder operations.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int minOperations(vector<string>& logs) {
int res = 0;
for(auto& log : logs) {
if(log == "../") res = max(0, res - 1);
else if(log != "./") res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/07/10/PS/LeetCode/crawler-log-folder/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.