You are viewing a single comment's thread. Return to all comments →
C++ Solution:
vector<int> getMax(vector<string> operations) { stack<int> s; stack<int> maxStack; vector<int> results; for(auto item : operations){ string op; string val; stringstream ss(item); ss >> op >> val; switch (stoi(op)) { case 1: s.push(stoi(val)); if(!maxStack.empty() && maxStack.top()<=stoi(val)) maxStack.push(stoi(val)); if(maxStack.empty()) maxStack.push(stoi(val)); break; case 2: if(!maxStack.empty() && !s.empty() && maxStack.top() == s.top()) maxStack.pop(); if(!s.empty()) s.pop(); break; case 3: if(!maxStack.empty()) results.push_back(maxStack.top()); else results.push_back(0); break; } } return results; }
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
C++ Solution: