The beauty of programming---to find the longest increment subsequence in an array

Source: Internet
Author: User

Write a program that is as low in complexity as possible, and find the longest increment of the length of a subsequence in a one-dimensional array (n elements).

Solution One: Use dynamic programming to find out the maximum increment subsequence length at the end of the current element. Dp[i+1] = Max{1, dp[i]+1}, Array[i+1]>array[k],k<=i; The complexity is O (N*n + N).

Solution Two: Open an array to save the length of the increment sub-sequence of the maximum length of the last element of the smallest value, and then when the first element of the array is processed, the length of the current maximum subsequence starts to decrement, and then search until Arrary[i] is greater than the current maximum subsequence length of the end element value, Then update the length of the subsequence at the end of the first element. If the current longest sequence is greater than the longest increment sequence length, update the longest information, otherwise see if you want to update because Array[i] is added so that the elements at the end of the same length of the end element with Array[i] are the lowest element value. The complexity of this method is still O (n*n).

Solution three: Because in the increment sequence, if the i<j, then the length of the subsequence of the end element value must be less than the length of the end of J element value. Therefore, for finding the value of the end element until Arrary[i] is greater than the current maximum subsequence length, the idea of dichotomy can be taken, and the complexity is reduced to O (N*logn).

The beauty of programming---to find the longest increment subsequence in an array

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.