300. Longest Increasing Subsequence

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,其实就是懒。 大笑

猜你喜欢

转载自blog.csdn.net/qq_36718317/article/details/79808633