greedy October 11, 2022

Increasing triplet subsequence

Time O(n) Space O(1) Open original problem

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)