美文网首页
300. Longest Increasing Subseque

300. Longest Increasing Subseque

作者: codingXue | 来源:发表于2016-06-06 11:54 被阅读128次

    问题描述

    Given an unsorted array of integers, find the length of longest increasing subsequence.
    For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
    Your algorithm should run in O(n2) complexity.
    Follow up: Could you improve it to O(n log n) time complexity?

    问题分析

    1. 动规,复杂度O(n2)**
    2. 覆盖方法,复杂度O(n log n)**
      参考了这篇文章
      这种方法的解题步骤是:
      -将第1个数字加入解集;
      -依次读取后面的数字,如果此数字比解集中最后一个数字大,则将此数字追加到解集后,否则,用这个数字替换解集中第一个比此数字大的数,解集是有序的,因此查找可以用二分法,复杂度O(log n);
      -最后的答案是解集的长度(而解集中保存的并不一定是合法解)。
      举个栗子,输入为[1,4,6,2,3,5]
      -解集初始化为[1];
      -读到4,将其追加到解集中,解集变为[1,4];
      -读到6,将其追加到解集中,解集变为[1,4,6];
      -读到2,用其替换解集中的4,解集变为[1,2,6],注意此时解集不是一个合法解,因为2是在6后出现的,但是解集的长度始终标识着当前最长序列的长度;
      -读到3,用其替换解集中的6,解集变为[1,2,3];
      -读到5,将其追加到解集中,解集变为[1,2,3,5],得到答案为解集长度4。

    【两种方法效率差异很大!】

    AC代码

    动规
    Runtime: 1068 ms, which beats 47.56% of Python submissions.

    class Solution(object):
        def lengthOfLIS(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            if n == 0:
                return 0
            rst = [-1 for i in range(n)]
            rst[-1] = 1
            opt = 1
            for i in range(n-2, -1, -1):
                m = 1
                for j in range(i+1, n):
                    if nums[j] > nums[i] and rst[j] + 1 > m:
                        m = rst[j] + 1
                rst[i] = m
                if m > opt:
                    opt = m
            return opt
    

    覆盖
    Runtime: 44 ms, which beats 94.09% of Python submissions.

    class Solution(object):
        def lengthOfLIS(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            if n == 0:
                return 0
            rst = [nums[0]]
            for i in range(1, n):
                if nums[i] > rst[-1]:
                    rst.append(nums[i])
                else:
                    index = self.midSearch(rst, nums[i])
                    rst[index] = nums[i]
            return len(rst)
    
        def midSearch(self, s, k):
            p = 0
            q = len(s) - 1
            while(p <= q):
                m = (p + q)/2
                if s[m] == k:
                    return m
                if s[m] > k:
                    q = m - 1
                else:
                    p = m + 1
            return p
    

    相关文章

      网友评论

          本文标题:300. Longest Increasing Subseque

          本文链接:https://www.haomeiwen.com/subject/aunrdttx.html