You are viewing a single comment's thread. Return to all comments →
Here's a C++ implementation of the logic that Josh_ gave, passes all the test cases.
int main() { stack<int> myStack; int n, q, max = 0, x; cin >> n; for (int i = 0; i < n; i++) { cin >> q; if (q == 1) { cin >> x; if (x > max) { myStack.push(x); max = x; } else myStack.push(max); } else if (q == 2) { myStack.pop(); if (myStack.empty()) max = 0; else if (myStack.top() != max) max = myStack.top(); } else if (q == 3) { cout << myStack.top() << endl; } } return 0; }
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 →
Here's a C++ implementation of the logic that Josh_ gave, passes all the test cases.