Increasing Triplet Subsequence - Medium - L
Published:
Question
- https://leetcode.com/problems/increasing-triplet-subsequence/
Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.
Approach
- take account of min number and seq length
Solution
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int n = nums.size();
if(n<=2) {
return false;
}
int min_num = nums[0];
int store_min = nums[0];
int second = INT_MIN;
int seq = 1;
for(int i=1; i<n; i++) {
if (min_num == nums[i]) {
continue;
}
if (min_num > nums[i]) {
min_num = nums[i];
continue;
}
if (nums[i] < second) {
second = nums[i];
store_min = min_num;
} else if (nums[i] > second) {
seq++;
if(seq == 3) {
return true;
}
second = nums[i];
}
}
return false;
}
};