Share Python easy understand Solution using bisect O(N logN)


  • 0
    F
    class Solution(object):
        def lengthOfLIS(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums: return 0
            sequence = [nums[0]]
            for i in xrange(1, len(nums)):
                if nums[i] > sequence[-1]:
                    sequence.append(nums[i])
                else:
                    idx = bisect.bisect_left(sequence, nums[i]) #find the left most position to replace nums[i] O(logN)
                    sequence[idx] = nums[i]
            return len(sequence)
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.