300. Longest Increasing Subsequence

本文介绍了一种求解最长递增子序列(LIS)问题的两种算法:动态规划和二分查找法。动态规划方法的时间复杂度为O(n^2),而二分查找法通过维护一个动态更新的有序数组,将时间复杂度降低到O(n log n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.再遍历数组,对于nums[i]>nums[j],i>j的,那么len[i]=max(len[i],len[j]+1),最后求最大的len即为最长的长度。时间复杂度为O(n^2)

二、二分法

In this approach, we scan the array from left to right. We also make use of a dpdp array initialized with all 0's. This dpdparray is meant to store the increasing subsequence formed by including the currently encountered element. While traversing the numsnums array, we keep on filling the dpdp array with the elements encountered so far. For the element corresponding to the j^{th}jth index (nums[j]nums[j]), we determine its correct position in the dpdp array(say i^{th}ith index) by making use of Binary Search(which can be used since the dpdp array is storing increasing subsequence) and also insert it at the correct position. An important point to be noted is that for Binary Search, we consider only that portion of the dpdp array in which we have made the updations by inserting some elements at their correct positions(which remains always sorted). Thus, only the elements upto the i^{th}ith index in the dpdp array can determine the position of the current element in it. Since, the element enters its correct position(ii) in an ascending order in the dpdp array, the subsequence formed so far in it is surely an increasing subsequence. Whenever this position index ii becomes equal to the length of the LIS formed so far(lenlen), it means, we need to update the lenlen as len = len + 1len=len+1.

Note: dpdp array does not result in longest increasing subsequence, but length of dpdp array will give you length of LIS.

Consider the example:

input: [0, 8, 4, 12, 2]

dp: [0]

dp: [0, 8]

dp: [0, 4]

dp: [0, 4, 12]

dp: [0 , 2, 12] which is not the longest increasing subsequence, but length of dpdp array results in length of Longest Increasing Subsequence.


代码:

class Solution {
    public int lengthOfLIS(int[] nums) {
        int []len=new int[nums.length];
        // if(nums.length==0)
        //     return 0;
        // for(int i=0;i<nums.length;i++)
        //     len[i]=1;
        // int max_len=1;
        // for(int i=1;i<nums.length;i++)
        // {
        //     for(int j=0;j<i;j++)
        //     {
        //         if(nums[i]>nums[j])
        //             len[i]=Math.max(len[i],len[j]+1);
        //     }
        //     max_len=Math.max(len[i],max_len);
        // }
        // return max_len;
        
        int l=0;
        for(int num:nums)
        {
            int i=Arrays.binarySearch(len,0,l,num);
            if(i<0)
                i=-(i+1);
            len[i]=num;
            if(l==i)
                l++;
        }
        return l;
    }
}
PS:Java的binarySearch函数可以参考https://www.cnblogs.com/qingergege/p/5658292.html。不得不说比c++方便多了,hhhh,其实就是懒。大笑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值