DEV Community

Cover image for Video + Solution: Leetcode Increasing Triplet Subsequence
shubhsheth
shubhsheth

Posted on

Video + Solution: Leetcode Increasing Triplet Subsequence

Solution

bool IncresingTripletSubsequence (vector<int> &nums) {
  int a = INT_MAX;
  int b = INT_MAX;

  for (int num : nums) {
    if (num <= a) {
      a = num;
    } else if (num <= b) {
      b = num;
    } else {
      return true;
    }
  }

  return false;        
}
Enter fullscreen mode Exit fullscreen mode

Complexity

Runtime: O(n)
Space: O(1)

Discussion (0)