MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / minOperations

Method minOperations

C++/Crawler-Log-Folder.cpp:3–17  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
3 int minOperations(vector<string>& logs) {
4 stack<string> s; //stack s is initialized and stack is empty when you are on main folder
5 for(int i=0;i<logs.size();i++){
6 if (logs[i]=="../" && !s.empty()){ //if string is "../" and stack is not empty then go back one folder
7 s.pop();
8 }
9 else if((logs[i]=="./") || (logs[i]=="../" && s.empty())) { //if string is "./" or stack is empty and string is "../" then do nothing
10 continue;
11 }
12 else { // otherwise push the new folder on top of stack
13 s.push(logs[i]);
14 }
15 }
16 return s.size(); //number of steps required to go back to main folder will be equal to the number of elements remaining in the stack
17 }
18};

Callers

nothing calls this directly

Calls 2

popMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected