Increasing triplet subsequence

October 11, 2022

greedy

Problem URL: Increasing triplet subsequence

We will keep track of the smallest and second smallest number in the array. If we find a number that is greater than both, we return true. Otherwise, we return false.

class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        first = second = math.inf
        for third in nums:
            if third <= first:
                first = third
            elif third <= second:
                second = third
            else:
                return True
        return False

Time Complexity: O(n)
Space Complexity: O(1)