Leetcode 300. Longest increasing subsequence--longest ascending subsequence (Java)

Source: Internet
Author: User

Given an unsorted array of integers, find the length of longest increasing subsequence.

Example:

Note:

    • There may being more than one LIS combination, it's only necessary for your 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?

Solution One, O (n2):

Intuitively, select an element that has a number of elements that are smaller than his value at the end of this element, and then the ascending sub-sequence that ends with it is the number plus one. That is, the current state can be deduced from one or more of the previous states, so you can use dynamic planning. Establishes a one-dimensional array dp,dp[i] that represents the longest ascending subsequence length ending with nums[i]. The initial position is set to 1. For each element of the original array, the double loop traverses the original array from the beginning, and whenever a value smaller than the current element is found, the proof can form at least one dp[j]+1 ascending subsequence, so dp[i] = max (Dp[i], dp[j] + 1), and DP[J] has been evaluated before.

Solution Two, O (n log n):

Or think of "artificial" to do this problem is what process. Follow the example above to analyze:

First See 10, add a selection, the alternate set is {10};

After seeing 9, no ascending sequence is formed, then 9 should not be added to the alternate set. But since 9 is less than 10, replacing 10 with 9 increases the chance of the next ascending sequence, and does not affect the number of elements in the alternate set (because it is a replacement), so replace it with {9};

Encounter 2 reason ibid, replace 9, prepare a selection to become {2};

Encountered 5, this time formed an ascending sequence, it should be added to the alternate set, to become {2,5};

Encountered 3, did not form an ascending sequence, but still the case of adding 9, if at this time to replace 5 to 3, will increase the chance to form ascending sequence, and the preparation of the selection keep rising, and the number has not changed, so replace 5, prepare a selection to become {2,3};

Encounter 7, the same encounter 5, add elements, prepare the selected collection {2,3,7};

Encountered 101, ibid., preparation of selected works {2,3,7,101};

Encountered 18, still the same, although not formed ascending sequence, but if the 101 is replaced, then the opportunity to form the ascending sequence will increase, and the selection of the rising property and the number of elements are unchanged, so replace, the preparation of the selected set to {2,3,7,18}.

Now that all elements have been added, the number of elements to be selected is the longest ascending subsequence length. But note here that the elements in the collection are not the elements of the last oldest sequence. Because in the process of searching for the eldest son sequence, the goal is to increase the chance of forming the ascending sequence as far as possible, so the substitution is made.

After the "artificial" is made, it is good to use the program to realize the thinking process. To sum up is:

If the encountered element is larger than the elements in the alternate set, then it is added, which increases the length of the ascending sequence;

If the element encountered is smaller than the last element in the alternate collection, then it cannot be added to the prepared selection. But in order to increase the chance of getting the ascending sequence later, instead of destroying the elements of the set ascending attribute and the total number of elements, replace the element that is the smallest in the element that is larger than his, so that the condition can be satisfied.

At this point, the discovery of the collection has been kept in order, the search for replacement elements can be used to find a binary, O (n log n) time complexity. It is also important to note that if an element is already in an alternate set, it does not require any action because it does not increase the length of the ascending sequence, nor does it increase the chance of encountering an ascending sequence, so skip directly.

Java (Solution one)

classSolution { Public intLengthoflis (int[] nums) {        if(Nums = =NULL|| Nums.length = = 0)return0; int[] DP =New int[Nums.length]; intmax = 1;  for(inti = 0; i < nums.length; i++) Dp[i]= 1;  for(inti = 0; i < nums.length; i++) {             for(intj = 0; J <= I; J + +) {                if(Nums[j] <Nums[i]) {Dp[i]= Math.max (Dp[i], 1 +Dp[j]); Max= max > Dp[i]?Max:dp[i]; }                        }        }        returnMax;}

Java (Solution II)

classSolution { Public intLengthoflis (int[] nums) {        if(Nums = =NULL|| Nums.length = = 0)return0; List<Integer> DP =NewArraylist<>(); Dp.add (nums[0]);  for(inti = 1; i < nums.length; i++) {            if(Dp.contains (Nums[i]))Continue; Else if(Nums[i] > Dp.get (dp.size ()-1) ) Dp.add (Nums[i]); Else if(Nums[i] < Dp.get (Dp.size ()-1)) {                intL = 0, r = dp.size ()-1;  while(L <r) {intMID = L + (r-l)/2; if(Dp.get (mid) < nums[i]) L = mid + 1; ElseR =mid;            } dp.set (R, Nums[i]); }        }        returndp.size (); }}

Leetcode 300. Longest increasing subsequence--longest ascending subsequence (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.