Transferred from:
Http://zhedahht.blog.163.com/blog/static/2541117420116135376632/
Problem description:
In the array, subtract the number on the right of the number to get a number pair. Calculate the maximum value of the difference between all number pairs. For example, in the array {2, 4, 1, 16, 7, 5, 11, 9}, the maximum value of the number pair is 11, which is the result of 16 minus 5.
Solution 1: Divide and conquer
We can imagine that the maximum value of the difference between the number pair is only one of the following three situations: (1) Both the subtrahend and the subtrahend are in the first sub-array, that is, the maximum value of the number pair difference in the first sub-array; (2) the difference between the number pair is in the second sub-array, that is, the maximum value of the number pair difference in the second sub-array; (3) the subarray is the maximum value of the first subarray. In the second child array, it is the minimum value of the second child array. The difference between the three values is the maximum value of the number pair deviation in the entire array.
// Find the # include <iostream> using namespace STD; int maxdiffcore (int * Start, int * end, int * max, int * min) in an array) {If (END = Start) {* max = * min = * start; return 0x80000000;} int * middle = start + (end-Start)/2; int maxleft, minleft; // recursive call. The maximum difference int leftdiff = maxdiffcore (START, middle, & maxleft, & minleft) between the left half is obtained. Int maxright, minright; // recursive call to obtain the maximum difference int rightdiff = maxdiffcore (middle + 1, end, & M Axright, & minright); // obtain int crossdiff = maxleft-minright; * max = (maxleft> maxright )? Maxleft: maxright; * min = (minleft <minright )? Minleft: minright; int maxdiff = (leftdiff> rightdiff )? Leftdiff: rightdiff; maxdiff = (maxdiff> crossdiff )? Maxdiff: crossdiff; return maxdiff;} int maxdiff_solution1 (INT numbers [], unsigned length) {If (numbers = NULL | length <2) {return 0;} int Max, min; return maxdiffcore (numbers, numbers + Length-1, & MAX, & min);} void main () {int A [] =, 11,9}; int B = maxdiff_solution1 (A, sizeof (a)/sizeof (A [0]); cout <B <Endl ;}
Solution 2: convert to the largest and most problematic sub-array
If you enter an array numbers whose length is N, we first construct an auxiliary array diff whose length is n-1, and diff [I] is equal to numbers [I]-numbers [I + 1] (0 <= I <n-1 ). If we accumulate the number I from the array diff TO THE NUMBER J (J
> I), that is, diff [I] + diff [I + 1] +... + Diff [J] = (numbers [I]-numbers [I + 1]) + (numbers [I + 1]-numbers [I + 2]) +... + (numbers [J]-numbers [J + 1]) = numbers [I]-numbers [J + 1].
From the analysis, we can find the maximum number pair difference in the original array (that is, numbers [I]-numbers [J + 1]). it is actually the sum of the largest continuous sub-arrays in the auxiliary array diff.
// Find # include <iostream> using namespace STD; int maxdiff_solution2 (INT numbers [], unsigned length) {If (numbers = NULL | length <2) in an array) {return 0;} int * diff = new int [length-1]; for (INT I = 1; I <length; ++ I) {diff [I-1] = numbers [I-1]-numbers [I];} int currentsum = 0; int greatestsum = 0x80000000; For (INT I = 0; I <length-1; ++ I) {If (currentsum <= 0) {currentsum = diff [I];} else {currentsum + = diff [I];} if (currentsum> greatestsum) {greatestsum = currentsum ;}} Delete [] diff; return greatestsum;} void main () {int A [] = {2, 4, 11,9}; int B = maxdiff_solution2 (A, sizeof (a)/sizeof (A [0]); cout <B <Endl ;}
Solution 3: Dynamic Programming
We define diff [I] as the maximum difference between all the number pairs in the array whose I number is reduced. That is to say, for any H (H <I), diff [I] ≥number [H]-number [I]. The maximum value of diff [I] (0 ≤ I <n) is the difference of the maximum number pair of the entire array.
Suppose we have obtained diff [I]. How can we obtain diff [I + 1? For diff [I], there must be an H (H <
I), the difference between number [H] And number [I] is the largest, that is, number [H] should be the maximum value of all numbers before number [I. When we calculate diff [I + 1], we need to find the maximum value before I + 1. There are two possibilities for the maximum value before the I + 1 number: the maximum value may be the maximum value before the I number, or the maximum value may be the I number. The maximum value before the I + 1 number must be greater than the two. We can get diff [I + 1] by subtracting the maximum value before the number I + 1 from number [I + 1].
// Find # include <iostream> using namespace STD; int maxdiff_solution3 (INT numbers [], unsigned length) {If (numbers = NULL | length <2) in an array) {return 0;} int max = numbers [0]; int maxdiff = max-numbers [1]; for (INT I = 2; I <length; ++ I) {If (numbers [I-1]> MAX) {max = numbers [I-1];} int currentdiff = max-numbers [I]; If (currentdiff> maxdiff) {maxdiff = currentdiff;} return maxdiff;} void main () {int A [] = {,}; int B = maxdiff_solution3 (, sizeof (a)/sizeof (A [0]); cout <B <Endl ;}