-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path334. Increasing Triplet Subsequence.cpp
29 lines (25 loc) · 1.15 KB
/
334. Increasing Triplet Subsequence.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
// Problem Link : https://leetcode.com/problems/increasing-triplet-subsequence/
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
priority_queue<int> small; // it will store elements in the left of current
priority_queue<int,vector<int>,greater<int>> large; // it will store elements in the right of current
vector<pair<bool,bool>> small_large(nums.size());
for(int i = nums.size()-1; i >= 0; i--){
// if there is a smaller element on the left
if(small.size() > 0 && small.top() > nums[i])
small_large[i].second = true;
small.push(nums[i]);
}
for(int i = 0; i < nums.size(); i++){
// if there is a larger element on the right
if(large.size() > 0 && large.top() < nums[i])
small_large[i].first = true;
large.push(nums[i]);
// if left side has atleast one smaller and right side has atleast one larger element
if(small_large[i].first == true && small_large[i].second == true)
return true;
}
return false;
}
};