Longest ascending subsequence

Source: Internet
Author: User
Longest ascending subsequence

Problem:

Returns the length of the longest incrementing subsequence in a one-dimensional array.


Solution 1:

It is obvious that the dynamic planning algorithm is used to select the following stages (this method is extremely common), so that the relationship between stages has no aftereffect.

Phase: in all subarrays ending with Element k, select the longest ascending subsequence, k = 1, 2... n.
Status: there is only one longest ascending subsequence in the longest ascending subsequence ending with Element k.

Decision: The longest incrementing subsequence that determines the end of Element k has a way to obtain the K-1, and the longest incrementing subsequence that ends with any element may become part of it. The time complexity is O (n ^ 2), and the space complexity is O (n)
Solution 2: The time complexity of dynamic planning is generally the same as that of spatial complexity. Since all the conditions for determining the next stage are stored in dp, in the processing process, we can process the obtained conditions to reduce the time complexity. Here, the time complexity is higher than the space complexity by O (n), indicating that there is still room for further optimization.
We can count the length of the longest incrementing subsequence in all the previous stages, and divide the longest incrementing subsequence with the same length into the same group, and only record the minimum value of their maximum elements MaxV [length value]. Suppose that the element of stage k is greater than the minimum value of the sub-sequence with the maximum length of I increment MaxV [I], then the length of the longest incrementing subsequence of phase k must be at least I + 1.
However, we found that the MaxV array has a very good incremental relationship (dynamic planning is used to solve the optimization problem, and we can always sort the previous statistical results through the optimization relationship ), that is, if I <j, MaxV [I] <MaxV [j] is available, and then the position of k elements in the MaxV array is used in the binary lookup method. Proof: If I <j <= k, there is a MaxV [I]> = MaxV [j], there is an incremental sequence a1a2 with the length of I... ai, and ai is the minimum value of the maximum element in the incremental sequence where all the lengths are I when k is calculated, and the sequence b1b2 with the length of j... bj and ai> = bj, because I <j length j sequence b1b2... bj contains a subsequence b1b2... bi, and bi <bj <= ai, that is, the minimum value of the maximum element of an incremental sub-sequence with the length of I is not ai. Code:
# Include <iostream> # include <cstring> # include <algorithm> # define MAX 1000 using namespace std; int dp [MAX]; int LIS1 (int Array [], int n) {int I, j; int max =-0 xfffff; for (I = 1; I <= n; I ++) {dp [I] = 1; for (j = 1; j <I; j ++) {if (Array [I]> Array [j] & dp [j] + 1> dp [I]) dp [I] = dp [j] + 1;} if (dp [I]> max) max = dp [I];} return max ;} int MinValue (int Array [], int n) {int min = 0 xFFFFFFF; for (int I = 1; I <= n; I ++) if (min> Array [I]) min = Array [I]; return min;} int LIS2 (int Array [], int n) {int MaxV [MAX]; // record the incremental sequence information in the Array MaxV [0] = MinValue (Array, n)-1; // The boundary value, the minimum value in the Array MaxV [1] = Array [1]; // boundary value, the first value in the array for (int I = 1; I <= n; I ++) // initialize the information of the longest incremental sequence dp [I] = 1; int nMaxLIS = 1; // The initial value of the longest incremental subsequence of the array for (int I = 1; I <= n; I ++) {// traverses the information of the longest ever increasing sequence int j; for (j = nMaxLIS; j> = 0; j --) {if (Array [I]> MaxV [j]) {dp [I] = j + 1; break ;}// assume that the current longest sequence is longer than the longest increasing sequence length, update the longest information if (dp [I]> nMaxLIS) {nMaxLIS = dp [I]; MaxV [dp [I] = Array [I];} else if (MaxV [j] <Array [I] & Array [I] <MaxV [j + 1]) MaxV [j + 1] = Array [I];} return nMaxLIS;} int main (int argc, char * argv []) {int Array [MAX]; int I, n; cout <"Please input n = "; cin> n; cout <"Please input Array values:" <endl; for (I = 1; I <= n; I ++) cin> Array [I]; cout <"LIS1:" <LIS1 (Array, n) <endl; memset (dp, 0, sizeof (dp )); cout <"LIS2:" <LIS2 (Array, n) <endl; return 0 ;}

Solution 3: I wrote a binary search solution. Time complexity O (n * lgn): The longest incrementing subsequence.

Longest ascending subsequence

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.