LeetCode--300. 最长上升子序列(python3实现)

题目:给定一个无序的整数数组,找到其中最长上升子序列的长度。

示例:

输入: [10,9,2,5,3,7,101,18]
输出: 4 
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4.

解析思路:

建立一个临时数组new_arr(用于存放一组最长上升子列),首先将nums[0]插入其中,然后遍历nums[1:]

  • 如果遍历到的元素val <= new_arr[0],我们更新new_arr[0]=val(也就是遍历到的元素比最长上升子序列中的最小元素小,我们通过贪心的策略,当然是越小越好,所以替换之前元素)
  • 如果new_arr[0] < val <= new_arr[-1],我们通过二分搜索法找到第一个>=val的元素位置,然后用val替换掉它(和上面的做法相同)
  • 如果new_arr[-1] < val,我们更新new_arr.append(val)(也就是新加入的元素比最长上升子序列中的最后一个元素大,那么必然构成解)

    ---------------------

    作者:coordinate_blog

    来源:CSDN

    原文:https://blog.csdn.net/qq_17550379/article/details/82871892

实现代码:

class Solution:
    def lengthOfLIS(self, nums) -> int:
        length = len(nums)
        new_arr = [] # 最长上升子列数组
        if length == 0:  # 原数组为空
            return 0
        elif length == 1:  # 原数组只有一个元素
            return 1
        else:
            new_arr.append(nums[0])
            for i in range(1, length):
                if nums[i] <= new_arr[0]:  # 如果遍历的元素比子列中最小值还小或相等,则替换
                    new_arr[0] = nums[i]
                elif (new_arr[0] < nums[i]) and (nums[i] <= new_arr[-1]):  # 如果遍历的元素值大小在子列中间,则查找到第一个大于或等于此元素的子列元素,进行替换;new_arr目前是已经有序的,所以可以用二分查找提高检索效率
                    low,high = 0,len(new_arr)-1
                    while low <= high:
                        mid = (low+high)//2
                        if new_arr[mid] >= nums[i]:
                            new_arr[mid] = nums[i]
                            break
                        else:
                            low = mid + 1
                elif nums[i] > new_arr[-1]:  # 如果遍历的元素比子列最大元素还大,则追加到子列尾部
                    new_arr.append(nums[i])
            return len(new_arr)

执行用时 : 52 ms, 在Longest Increasing Subsequence的Python3提交中击败了96.97% 的用户
内存消耗 : 13.1 MB, 在Longest Increasing Subsequence的Python3提交中击败了96.54% 的用户

代码仅供参考

猜你喜欢

转载自www.cnblogs.com/paopaolx/p/10845866.html