forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
39 lines (31 loc) · 807 Bytes
/
main2.cpp
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
/// Source : https://leetcode.com/problems/validate-stack-sequences/
/// Author : liuyubobobo
/// Time : 2018-11-25
#include <iostream>
#include <vector>
#include <stack>
#include <unordered_set>
using namespace std;
/// Using a stack to simulation
/// Greedy Thinking is used when deal with popped elements
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> stack;
int i = 0;
for(int e: pushed){
stack.push(e);
while(i < popped.size() && !stack.empty() && popped[i] == stack.top()){
stack.pop();
i ++;
}
}
return i == popped.size();
}
};
int main() {
return 0;
}