Skip to content

Commit e0eee92

Browse files
Create 132_pattern.cpp
1 parent ffe3973 commit e0eee92

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

132_pattern.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
public:
3+
bool find132pattern(vector<int>& nums) {
4+
// completely wrong first initial thoughts !!!
5+
//how can i apply sliding window??, there's negative bounds defined!
6+
/* int size = nums.size();
7+
if (size <= 1) return false;
8+
9+
int start = 0, end = 1;
10+
while(end < size) {
11+
if(nums[start] < nums[end]) { //start < end
12+
if(end >= size - 1) {
13+
return false;
14+
}
15+
if(nums[end + 1] < nums[end] and nums[end + 1] < nums[start]) { //next_to_end < end
16+
cout<<nums[start]<<nums[end]<<" "<<nums[end + 1]<<"\n";
17+
return true;
18+
}
19+
}
20+
start++, end++;
21+
}
22+
return false;
23+
*/
24+
25+
/*
26+
nums[k] < nums[j]
27+
nums[k] > nums[i]
28+
nums[j] > nums[i]
29+
30+
finding nums[k] is the problem! This was a bit difficult to figure out - monotonic decreasing sequence.
31+
*/
32+
33+
34+
stack<pair<int, int>> st;
35+
int currentMin = nums[0];
36+
37+
38+
for(int i = 1; i < nums.size(); ++i) {
39+
while(!st.empty() and nums[i] >= st.top().first) {
40+
st.pop();
41+
}
42+
if(!st.empty() and nums[i] < st.top().first and nums[i] > st.top().second) {
43+
return true;
44+
}
45+
46+
st.push({nums[i], currentMin});
47+
currentMin = min(currentMin, nums[i]);
48+
}
49+
return false;
50+
51+
}
52+
};

0 commit comments

Comments
 (0)