-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCrawler Log Folder.cpp
47 lines (45 loc) · 1.15 KB
/
Crawler Log Folder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//1.using stack
class Solution {
public:
int minOperations(vector<string>& logs) {
if(logs.size()==0) return 0;
stack<string> st;
for(auto x: logs){
if (x[0] != '.') //Move to the child folder so add children
st.push(x);
else if(x=="../"){ // Move to the parent folder of the current folder so pop
if(!st.empty()) st.pop();
else continue; //don’t move the pointer beyond the main folder.
}
}
return st.size();
}
};
//2.
class Solution {
public:
int minOperations(vector<string>& logs) {
int ans = 0;
for (string log : logs) {
if (log == "../") { // go deeper
ans--;
ans = max(ans, 0);
} else if (log != "./") // one level up
ans++;
}
return ans;
}
};
//3.
class Solution {
public:
int minOperations(vector<string>& logs) {
int res = 0;
for (string s : logs) {
if (s=="../") res = max(0, --res);
else if (s=="./") continue;
else res++;
}
return res;
}
};