Written programming problem must kill skill--dynamic programming

Source: Internet
Author: User

In the written programming problem, the most common type of question is the dynamic planning, has not been very clear, today determined to well comb.

Dynamic programming is solved by combining sub-problems to solve the whole problem, a big problem decomposed into a small problem, the small problem is divided into small problems, and so on, until the final results.

First look at a problem that has abused me countless times: the problem of the largest subarray.

Title: An array of n integer elements (a[0], a[1],..., a[n-1], a[n]), and this array of course has a lot of sub-arrays, so what is the maximum value of the sum of the array?

For example: There is an array of int a[5] = {-1, 2, 3,-4, 2}; the qualifying Subarray is 2, 3, and the final answer is 5.

Clear the test instructions: 1, the subarray must be contiguous.

2, do not need to know what the final sub-array meets the requirements of what exactly, in what position.

3, the array can contain positive numbers, negative numbers, 0.

First, let's take a look at the simplest, and I never would have. Poor lifting method (ashamed ing):

public static int Findmaxsubarray (int[] A, int n) {int max = integer.min_value;for (int. i=0; i<n; i++) {int sum = 0;for (in T j=i; j<n; J + +) {sum + = a[j];if (Sum > Max) {max = sum;}}} return Max;}

This method is simple and rough, after each sub-array is calculated, find the maximum value.

The complexity of Time is O (n^2).

Will such a method be a little low, interviewer would like it? Of course not! This question they want to test your dynamic planning!!!

Let's take a look at the topic, considering the relationship between the first element of the array and the largest array, a[0] and (A[i],..., a[j]), as follows:

1, when 0=i=j, the element A[0] is the largest paragraph

2, when 0=i<j, and the largest section from A[0] Start

3, when 0<i, element A[0] and the largest paragraph is not related

You can convert a large problem (an array of N elements) into a smaller problem (an array of N-1 elements), if you Know (A[1],...,a[n-1]) and the largest array after all[1], and Know (A[1],...,a[n-1]) contains a[1] and the largest array of start[1], then it is not difficult to conclude that the answer to the question in (A[1],...,a[n]) all[0] = max{a[0], a[0]+srart[1], all[1]}, through such an analysis, you may be blindfolded, but in short, A[0] is separated from the remaining elements, and if you know the sum after the largest subarray of the array with the remaining elements, then the largest sub-array of the entire array is followed by the A[0],a[0]+sum, which is the maximum value between the sum three.

</pre><pre name= "code" class= "java" >public static int dongtaifind (int[] A, int n) {int start = A[n-1];int max = a[n-1];for (int i=n-2; i>=0; i--) {start = Max (A[i], start+a[i]); max = max (start, max);} return Max;} public static int Max (int m, int n) {if (M >= N) return m;else {return n;}}

This question we are temporarily over:

Here's a look at the more classic 01 knapsack problem:

There are n items and a backpack with a capacity of V. The volume of article I is c[i], the value is v[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

We put the topic specific, there are 5 items, the size of the backpack is 10, their volume is c[5] = {3,5,2,7,4}; Value is v[5] = {2,4,1,6,5};

An item can be put or not put.

If the optimal solution of a problem contains the item n, that is Xn = 1, then the rest of X1, X2, ....., Xn-1 must constitute sub-problems,....., n-1 the optimal solution in capacity c-cn.

if the optimal solution does not contain item n, i.e. xn = 0; So the rest of X1, X2 ... Xn-1 must constitute the optimal solution of the sub-problem,.... n-1 at capacity c.

public static int MaxValue (int[] C, int[] v, int n, int capacity) {//c array represents volume, V array represents value, N represents number of goods, s represents backpack volume int max = 0;int[][] Tab Le = new int[n+1][capacity+1];for (int i=0; i<n; i++) {for (int k=0; k<capacity; k++) {table[i][k] = 0;}} for (int i=1, i<=n; i++) {for (int k=1; k<=capacity; k++) {if (c[i-1] > k) {table[i][k] = table[i-1][k];} else {Table[i][k] = max (Table[i-1][k], table[i-1][k-c[i-1]]+v[i-1]);}}} return table[n][capacity];}

Reference: http://www.cnblogs.com/bourbon/archive/2011/08/23/2151044.html

Written programming problem must kill skill--dynamic programming

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.