Subsequence with longest Increment

Source: Internet
Author: User

I. Problem Description

Set the sequence L = <A1, A2, A3 ,..., an> is a sequence whose length is N. An ascending sequence of L is described as: <aI1, ai2 ,..., AIK>, where the subscript sequence <I1, I2 ,..., ik> Yes

The sub-sequence <aI1, ai2,..., AIK> is also incremental. The length of this incremental sequence is K.

Solution 1: Converting to LCS

First, sort the sequence L in ascending order to get another sequence s, and then obtain the longest common subsequence of L and S.

Three solutions 2: Dynamic Planning

In addition, Len [I] indicates the length of the longest incrementing sub-sequence ending with element I. Finally, obtain max {Len [I ]}.

Instance <1, 3, 4, 2, 7>

Len [1] = 1. The longest incrementing subsequence ending with 1 is <1> and the length is 1.

Len [2] = 2. The longest incrementing subsequence ending with 3 is <1, 3> and the length is 2.

Len [3] = 3. The longest incrementing subsequence ending with 4 is <1, 3, 4>, and the length is 3.

Len [4] = 2. The longest incrementing sub-sequence ending with 2 is <1, 2> and the length is 2.

So how can we find Len [5? Into the following subproblem:

If 5th elements are equal to 7, find the element a [I] whose Sn is before a [5] and earlier than a [5]. the longest incrementing subsequence ending with a [I] plus a [5] to form a new longest incrementing subsequence.

The length is 1 longer than the original one. There are multiple (up to four) elements smaller than a [5], and the resulting incremental subsequence also has multiple, the longest one is equal to the longest incrementing subsequence ending with a [5.

In this example, a [3] is smaller than a [5], and the longest incrementing subsequence ending with a [3] is the longest, therefore, the length of the incremental sub-sequence ending with a [5] is incremented by 1 to 4.

Len [5] = 4. The longest incrementing sub-sequence ending with 5 is <,> and the length is 4.

Solution 4 3: Improvement of solution 2

In solution 2, when Len [I] is obtained, find all the elements smaller than a [I] in a [I-1], and a [1],... A [I-1] Is unordered. The search speed is slow.

F [k] is the final element in the ascending subsequence with the length equal to K. The longer the length is, the larger the end element is. Therefore, F [k] is incremental.

Instance <1, 3, 4, 2, 7>

Len [1] = 1, F [1] = A [1] = 1

Len [2] = 2, F [2] = A [2] = 3

Len [3] = 3, F [3] = A [3] = 4

Len [4] = 2, F [2] = A [4] = 2 (updated f [2])

So how can we find Len [5?

F [1] = 1, F [2] = 2, F [3] = 4, a [5] = 7,

Find the ascending subsequence whose end element is smaller than a [5] and has the longest length. In this example,

F [3] = 4 indicates a child sequence with a length equal to 3. The end element of the Child sequence is 4, and the length of the Child sequence is the longest.

A [5] adds the length of the new sequence formed by this sequence to 4, and then updates f [4] = A [5] = 7 to indicate the length.

Child sequence equal to 4, whose end element is equal to 7

In the above step, find the child sequence with the smallest element and the longest length than a [I ].

For f [1],... f [K], looking forward from the back, the first element smaller than a [I] is the longest subsequence. When searching

You can use the binary search method. Therefore, it is faster than solution 1.

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.