LeetCode "Increasing Triplet Subsequence" !!

Here is the mind flow:

My intuition was to use Fenwick tree - that works but that's O(nlgn). That's too generalized and the magic number is 3 - that means we can apply some Greedy-like thoughts. Fenwick stores all numbers' info however we just need 2..

class Solution(object):
    def increasingTriplet(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        n = len(nums)
        if n < 3: return False

        c1, c2 = 0xFFFFFFFF, 0xFFFFFFFF
        for v in nums:
            if v <= c1:
                c1 = v
            elif v <= c2:
                c2 = v
            else:
                return True

        return False

你可能感兴趣的:(LeetCode "Increasing Triplet Subsequence" !!)