The path of data structure and algorithm learning: Dynamic programming algorithm and binary idea algorithm of lis--maximum increment sequence

Source: Internet
Author: User

The problem description of the longest increment sequence:

The longest increment subsequence of an integer sequence, and the subsequence is not required to be contiguous. For example:

Input:4,6,9,6,7,6,3,8,10;output:5


Second, the solution:

1, with dynamic planning method to solve. From the problem we can know that we finally get the longest increment subsequence, and any of its subsequence is also the oldest sequence in the corresponding sequence. This may not be a good idea, as the above example says:

The oldest oldest sequences are: 4, 6, 7, 8, 10. Select one of the subsequence sequences in this sequence, for example: 4,6,7. The 4,6,7 is also the oldest sequence of the 4,6,9,6,7,6,3 sequence.


For the dynamic programming idea, the most important thing is to find the state and state transition equations , so let's see how we get these two key things here:


Assuming that nums={4,6,9,6,7,6,3,8,10},f (x) represents the length of the oldest sequence in the sequence ending with the first X number in nums[x], then:


F (1) =1;f (2) =max{f (1) +1,1}=2;f (3) =max{f (2) +1,1}, ... And so on Someone here might ask, why is f (x-1) +1 and 1 compared? The reason is simple: f (2) For example, F (2) =f (+), and f (1) = 1, that is, the result of F (2) is associated with F (1), and F (2) itself has an initial value of 1, according to the question required F (2) The final value should be through F (1) The value of the operation and its initial value are selected for the larger one. And so on the back.


2, this method is very ingenious, the main idea is that, I don't care what your sequence is composed of, I only care about the length of this sequence is how much. So I create an array d[length],d[0] to store the longest sequence length, d[i] represents the end element of the sequence at the longest increment sequence length of I. Find the insertion position for the element by binary search, forming a longest sequence.


The key to this solution is that the longest incremental sequence we want to get is, in fact, the oldest sequence of the eldest, as long as the numbers in each position are small enough and orderly. The words may not be clear, you can feel what I mean by code.


Third, the code:


1, O (N^2) DP algorithm


int Lis1 (int* nums, int length) {int lis_len = 1;int d[9];for (int i = 0; i < length; ++i) {D[i] = 1;for (int j = 0; J &L T I ++J) if (Nums[i] >= nums[j] && d[i] < D[j] + 1) ++d[i];if (Lis_len < d[i]) Lis_len = D[i];} return Lis_len;}

2, O (NLOGN) Two-point thought algorithm

int Lis2 (int* nums, int length) {int D[9];int mid, left, right;d[0] = 1;d[1] = nums[0];for (int i = 1; i < length; ++i) {  left = 1;right = D[0];while (left <= right) {mid = (left + right)/2;if (Nums[i] > D[mid]) left = mid + 1;elseright = Mid-1;} D[left] = Nums[i];if (Left > D[0]) ++d[0];} return d[0];}


The path of data structure and algorithm learning: Dynamic programming algorithm and binary idea algorithm of lis--maximum increment sequence

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.