aboutsummaryrefslogtreecommitdiff
path: root/valid_stack_seq.cpp
blob: 62250db4a13c89ed5a1093d941e598c3f10472d8 (plain)
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
// 946. Validate Stack Sequences
#include "leetcode.h"

/*
 * given two integer arrays 'pushed' & 'popped' each with distinct
 * values, return true if this could have been the result of a sequence
 * of push and pop operations on an initially empty stack, or false otherwise
 */

class Solution {
public:
  bool validateStackSequences(vector<int> &pushed, vector<int> &popped) {
    stack<int> s;
    int i = 0;
    for (int x : pushed) {
      s.push(x);
      while (s.size() && s.top() == popped[i]) {
        s.pop();
        i++;
      }
    }
    return !s.size();
  }
};

int main() {
  Solution obj;
  vector<int> pushed = {1, 2, 3, 4, 5};
  vector<int> p1 = {4, 5, 3, 2, 1}, p2 = {4, 3, 5, 1, 2};
  printf("%d\n", obj.validateStackSequences(pushed, p1)); // expect: 1
  printf("%d\n", obj.validateStackSequences(pushed, p2)); // expect: 0
}