leetcode 300 Longest Increasing Subsequence rise longest sequence of dynamic programming

leetcode 300 Longest Increasing Subsequence rise longest sequence

leetcode March 2020 a daily question punch
is said to Huawei asked this question

Question:
a predetermined integer array a disorder, wherein the length of the longest found rising sequence.
Example:
Input: [10,9,2,5,3,7,101,18]
Output: 4
explained: the longest sequence is increased [2,3,7,101], its length is 4.
Note: There may be various combinations of longest rising sequence, you only need to output a corresponding length. The time complexity of your algorithm should be O (n2).
Advanced: You can reduce the time complexity of the algorithm to O (n log n) do?

Original title link: https: //leetcode-cn.com/problems/longest-increasing-subsequence

know how: Dynamic Programming

Suitable for solving the problem fixed rules:

  1. Problem with optimal substructure.
  2. No after-effect, multi-fingered find the optimal solution.

Moving gauge problem-solving ideas:

  1. Sub-division problem
  2. Determine the status
  3. Determine the state transition equation
  4. Determining a starting condition or the boundary condition

Recursive rules change, the recursive function is to contain the n parameters, into a n-dimensional array, the stored value is equal to the return value of the recursive function.

Thinking: Python dynamic programming, child [i] represents a sequence nums in the i-th element is the longest at the end of the sub-sequence length increased. Sub-problems: Before solving the i-th element is the longest sub-rise length of the sequence. Status: child [i]. State transition equation: each sub-question, through all the previous element nums [j], if nums [j] <nums [i ], the child [i] is equal to the maximum of all child [j] +1 in. Boundary conditions: a starting value of 1. Time complexity: O (n ^ 2), the number of dynamic programming state is n, when the calculation state, need O (n) time to traverse a state before, so the total time complexity is O (n ^ 2). Space complexity: O (n), because of the additional use of an array of length n.

detail:

  1. Exclude nums is empty
  2. best_i start value is set to 1 and can not be 0

Code:

class Solution(object):
    def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 动态规划 O(N^2)
        child=[] # child[i]表示以nums里第i个元素结尾的序列里最长上升子序列的长度
        if nums == []:
            return 0
        child.append(1)
        for i in range(1,len(nums)):
            best_i=1
            for j in range(0,i):
                if nums[i]>nums[j]:
                    tem=child[j]+1
                    if tem>best_i:
                        best_i=tem
            child.append(best_i)
        return max(child)

Method 2: greedy +Binary search: See the official explanations . The core idea of greedy algorithm is to find a local optimum. Time complexity O (NlogN). This question is only the length of the output, can be used instead of this method, such a sequence to be output, it can not be so.

class Solution(object):
    def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 贪心+二分查找 O(NlogN)
        if nums == []:
            return 0
        tail=[nums[0]] # 存长度最长上升子序列的最后一个元素值
        len=1 # 最长上升子序列的长度
        
        for num in nums[1:]:
            if num>tail[len-1]:
                tail.append(num)
                len+=1
            else:
                # 二分查找比num大的最小的元素及位置
                l=0
                r=len-1
                while l<r:
                    mid = int((l+r)/2)
                    if tail[mid] == num:
                        l=mid
                        break
                    if tail[mid]<num:
                        l=mid+1
                        continue
                    elif tail[mid]>num:
                        r=mid
                        continue
                tail[l]=num
            
        return len

PS. The second method is not typical, a first focus understood.
This blog is an original work, welcomed the guidance, reproduced, please indicate the source, attach a link to this article, thank you

Published 20 original articles · won praise 1 · views 206

Guess you like

Origin blog.csdn.net/weixin_43973433/article/details/104856044
Recommended