leetcode刷题之动态规划

  • 300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence.
    Example:
    Input: [10,9,2,5,3,7,101,18]
    Output: 4
    Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
    Note:
    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.

  • 思路一:
    在这里插入图片描述

class Solution(object):
    def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) < 1:
            return len(nums)
        dp = []
        dp.append(1)
        if len(nums) > 1:
            for i in range(1, len(nums)):
                dp.append(1)
                for j in range(i):
                    if nums[i] > nums[j] and dp[j] + 1 > dp[i]:
                        dp[i] = dp[j] + 1
        return max(dp)
  • 思路二:
    在这里插入图片描述
        if len(nums) <= 1:
            return len(nums)
        stack = []
        stack.append(nums[0])
        for i in range(1, len(nums)):
            if nums[i] > stack[-1]:
                stack.append(nums[i])
            else:
                for j in range(len(stack)):
                    if stack[j] >= nums[i]:
                        stack[j] = nums[i]
                        break
                        
        return len(stack)
  • 思路三
    在思路二中运用二分查找,因为二分查找的复杂度为O(logn),则最后的复杂度为O(nlogn)

        def binary_search(stack, val):
            begin = 0
            end = len(stack)
            while 1:
                mid = int(( begin + end ) / 2)
                if stack[mid] == val:
                    return mid
                elif stack[mid] < val:
                    if mid == len(stack)-1:
                        return mid
                    if val < stack[mid+1]:
                        return mid + 1
                    begin = mid + 1
                elif stack[mid] > val:
                    if mid == 0:
                        return 0
                    if val > stack[mid-1]:
                         return mid 
                    end = mid - 1
        
        if len(nums) <= 1:
            return len(nums)
        stack = []
        stack.append(nums[0])
        for i in range(1, len(nums)):
            if nums[i] > stack[-1]:
                stack.append(nums[i])
            else:
                index = binary_search(stack, nums[i])
                stack[index] = nums[i]
        return len(stack)
  • 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence.
    Example 1:
    Input: [1,3,5,4,7]
    Output: 2
    Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
    Example 2:
    Input: [2,2,2,2,2]
    Output: 5
    Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences’ length is 1, so output 5.
    Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.

  • 思路:
    在第一道例题的基础上再增加一个数组去专门记录有多少个这样的长度。数量应该在未增加之前所有的数量之和。
class Solution(object):
    def findNumberOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) < 2:
            return len(nums)
        dp = []
        count = []
        result = 0
        dp.append(1)
        count.append(1)
        for i in range(len(nums)):
            dp.append(1)
            count.append(1)
            for j in range(i):
                if nums[i] > nums[j]:
                    if dp[j] + 1 > dp[i]:
                        dp[i] = dp[j] + 1
                        count[i] = count[j]
                    elif dp[j] + 1 == dp[i]:
                        count[i] += count[j]
        
        for i in range(len(dp)-1):
            if dp[i] == max(dp):
                result += count[i]
        return result
    

猜你喜欢

转载自blog.csdn.net/m0_37327467/article/details/88370138
今日推荐