aboutsummaryrefslogtreecommitdiff
path: root/132_pattern.cpp
blob: a9f0c437fa79ce379b841de602c8e2a4987a2c85 (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
#include "leetcode.h"

class Solution {
public:
  bool find132pattern(vector<int> &nums) {
    int n = nums.size(), minI[n];
    stack<int> s;
    minI[0] = nums[0];
    for (int i = 1; i < nums.size(); i++)
      minI[i] = min(minI[i - 1], nums[i]);
    for (int i = nums.size() - 1; i >= 0; i--) {
      while (!s.empty() && s.top() <= minI[i])
        s.pop();
      if (!s.empty() && s.top() < nums[i])
        return true;
      s.push(nums[i]);
    }
    return false;
  }
};

int main() {
  vector<int> nums = {1, 3, 2, 4};
  Solution obj;
  for (int i = 0; i < nums.size(); i++)
    cout << nums[i] << ' ';
  cout << endl << "searching for '132'...\n";
  if (obj.find132pattern(nums))
    cout << "found";
  else
    cout << "not found";
}