Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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
posted on 2016-02-16 15:14  Tonix  阅读(130)  评论(0编辑  收藏  举报