-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path946.cpp
More file actions
32 lines (28 loc) · 738 Bytes
/
946.cpp
File metadata and controls
32 lines (28 loc) · 738 Bytes
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
// Problem : 946. Validate Stack Sequences
// Link : https://leetcode.com/problems/validate-stack-sequences/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int j = 0;
for(int x : pushed){
st.push(x);
while (!st.empty() && st.top() == popped[j]){
st.pop();
j++;
}
}
return j == pushed.size();
}
};
int main() {
Solution ob;
vector<int> pushed {1,2,3,4,5};
vector<int> popped {4,5,3,2,1};
cout << ob.validateStackSequences(pushed, popped);
return 0;
}