-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy path334. Increasing Triplet Subsequence.cpp
31 lines (25 loc) · 1.3 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
30
31
//Approach :
/*We need to check if any strictly increasing sequence of size 3 present in the array.
For this, we can keep iterating the given array from left to right, and changing our FirstMinimumValue and SecondMinimumValue.
If any number comes, which is greater than SecondMinimumValue then this shows that, we have found a triplet with increasing subsequence. => return true.
If after whole iteration, above statement is not returning any answer, then this shows no increasing triplet present in the array. => return false at the end.*/
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int FirstMinimumValue = INT_MAX,
SecondMinimumValue = INT_MAX;
if(nums.size()<3)
return false;
for(int& i : nums){
//checking if there is any minimum value before firstminimum
if(i <= FirstMinimumValue)
FirstMinimumValue = i; // assign i to firstminimum
else if(i <= SecondMinimumValue)
SecondMinimumValue = i;
// If any number comes, which is greater than SecondMinimumValue then this shows that, we have found a triplet with increasing subsequence. => return true.
else
return true;
}
return false;
}
};