Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c440eb2 commit 9065e70Copy full SHA for 9065e70
cpp/0946-validate-stack-sequences.cpp
@@ -0,0 +1,25 @@
1
+/*
2
+ Given two integer arrays pushed and popped each with distinct values,
3
+ return true if this could have been the result of a sequence of push
4
+ and pop operations on an initially empty stack, or false otherwise.
5
+
6
+ Time: O(n)
7
+ Space: O(n)
8
+*/
9
10
+class Solution {
11
+public:
12
+ bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
13
+ stack<int> stk;
14
+ int i = 0;
15
+ for (int num : pushed) {
16
+ stk.push(num);
17
+ while (!stk.empty() && stk.top() == popped[i]) {
18
+ stk.pop();
19
+ ++i;
20
+ }
21
22
+ return stk.empty();
23
24
+};
25
0 commit comments