class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> stack;
int i = 0; // popped's index
for (const int x : pushed) {
stack.push(x);
while (!stack.empty() && stack.top() == popped[i]) {
stack.pop();
++i;
}
}
return stack.empty();
}
};
leetcode
challenge
Here is the link for the problem:
https://leetcode.com/problems/validate-stack-sequences/
Top comments (0)