Data structure and Algorithm learning path: lis--dynamic programming algorithm for maximum increment sequence and binary thought algorithm _ Data structure and algorithm

Source: Internet
Author: User

The problem description of the longest increment sequence:

To find the longest ascending subsequence of an integer sequence, the subsequence is not required to be sequential. For example:

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

Second, the solution:

1, using dynamic planning method to solve. From the problem we can know that we eventually get the longest ascending subsequence, and any of its sequence is also the oldest sequence in the corresponding sequence. This may not be easy to understand, as in the example above:

The eldest child sequence is: 4, 6, 7, 8, 10. Select one of the sequences in this sequence, for example: 4,6,7. Then 4,6,7 is also the eldest child sequence of the 4,6,9,6,7,6,3 sequence.


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


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


F (1) =1;f (2) =max{f (1) +1,1}=2;f (3) =max{f (2) +1,1}, ... Analogy Maybe someone asked, why is F (x-1) +1 and 1 compared AH. The reason is simple: take F (2) as an example, F (2) =f (1+1), and f (1) = 1, that is, the result of F (2) is associated with F (1), whereas F (2) itself has an initial value of 1, the final value of F (2) According to the question is supposed to be through F (1) The value of the operation and its initial value select the larger one. And so on in the back.


2, this method is very ingenious, the main idea is this, I don't care what your sequence is composed of, I only care about the length of this sequence is how much. So I created an array d[length],d[0] used to store the longest sequence length, d[i] represents the end element of the sequence when the maximum increment sequence length is I. Through the two-point lookup constantly for the element to find the insertion position, constitute a longest sequence.


The key to this solution is that the maximum increment sequence we are going to obtain, in fact, is that if the number of digits in each position is small enough and orderly, then the most important sequence is the oldest. may not be very clear, you can use the code to feel what I mean.


Third, Code:


DP algorithm for 1, O (n^2)


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 < 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) binary 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;
			else right
				= mid-1;
		}
		D[left] = nums[i];
		if (Left > d[0])
			++d[0];
	return d[0];
}


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.