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?

Title:

Find the longest increasing subsequence.

Ideas:

1. Dynamic programming

Initially, each element is separate, and its length is 1. Then traverse the array, for nums[i]>nums[j], i>j, then len[i]=max(len[i], len[j]+1), and finally the largest len ​​is the longest length. The time complexity is O(n^2)

2. Dichotomy

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 lenl e n  as len = len + 1l e n = l e n + 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.


Code:

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's binarySearch function can refer to https://www.cnblogs.com/qingergege/p/5658292.html. I have to say that it is much more convenient than C++, hhhh, it is actually lazy. laughing out loud

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935237&siteId=291194637