-
Notifications
You must be signed in to change notification settings - Fork 0
/
05 Stack Permutations
61 lines (49 loc) · 1.15 KB
/
05 Stack Permutations
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//{ Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
int isStackPermutation(int N,vector<int> &A,vector<int> &B){
queue<int> q;
stack<int> s;
for(int i=0; i<N; i++){
q.push(A[i]);
}
for(int i=0; i<N; i++){
if(!s.empty() && s.top()==B[i]){
s.pop();
}else{
while(!q.empty() && q.front()!=B[i]){
s.push(q.front());
q.pop();
}
if(q.empty()) return false;
q.pop();
}
}
return true;
}
};
//{ Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> a(n),b(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
Solution ob;
cout<<ob.isStackPermutation(n,a,b)<<endl;
}
return 0;
}
// } Driver Code Ends