Question: The number in the array minus the number on the right (not adjacent) to get a number pair difference. Calculate the maximum value of the difference between all number pairs. For example, for an array {2, 4, 1, 16, 7, 5, 11, 9}, the maximum value of the number pair is 11, which is the result of 16-5.
Analysis 1: we can also use the Traversal method to fix a number and subtract the number on the right of the question one by one to get the difference between the number pairs and then select the maximum value; perform this operation on each number in the sequence, and then remove the maximum values of all values. However, as with all enumeration algorithms, such algorithms are less efficient, and there is only n * (n-1)/2 difference between all number pairs, therefore, the time complexity is O (n ^ 2 ).
Analysis 2: think of the idea of splitting a large character into two smaller strings in "algorithm 05" left-rotated strings ", and then processing them one by one. We will think: can we divide this array into two parts? Of course. We can divide the array into two subarrays, so the solution to this problem must exist in the following three situations:
(1) Both subtrahend and subtravers are in the first subarray, that is, the maximum value of the number pair of the first subarray; (2) the subtrahend and subtrahend are in the second subarray, that is, the maximum value of the number pair of the second sub-array; (3) the Sub-array is reduced to the first sub-array, and the sub-array is reduced to the maximum value of the first sub-array, subtraction is the minimum value in the second child array.
So what our program needs to do is to record the maximum value of the first sub-array and record the minimum value of the second sub-array. the maximum value of the difference between the number of each sub-array can be solved recursively, the smallest subproblem is that both the left and right subarrays have only one number. Based on this idea, we have the following code:
1 # include <iostream>
2 # include <string>
3 using namespace std;
4
5 int MaxDiffCore (int * start, int * end, int * max, int * min)
6 {
7 // minimum subproblem
8 if (start = end)
9 {
10 * max = * min = * start;
11 return 0;
12}
13
14 int * middle = start + (end-start)/2;
15
16 // returns the maximum, minimum, and maximum number pairs of the first sub-array.
17 int maxLeft, minLeft;
18 int leftDiff = MaxDiffCore (start, middle, & maxLeft, & minLeft );
19
20 // returns the maximum, minimum, and maximum number pairs of the first sub-array.
21 int maxRight, minRight;
22 int rightDiff = MaxDiffCore (middle + 1, end, & maxRight, & minRight );
23
24 // calculate the number of records that are dropped in the first subarray and in the second subarray
25 int crossDiff = maxLeft-minRight;
26
27 * max = (maxLeft> maxRight )? MaxLeft: maxRight;
28 * min = (minLeft <minRight )? MinLeft: minRight;
29
30 int maxDiff = (leftDiff> rightDiff )? LeftDiff: rightDiff;
31 maxDiff = (maxDiff> crossDiff )? MaxDiff: crossDiff;
32 return maxDiff;
33}
34
35
36 int MaxDiff (int array [], int arrayLength)
37 {
38 if (array = NULL | arrayLength <2)
39 return-1;
40
41 int max, min;
42 return MaxDiffCore (array, array + arrayLength-1, & max, & min );
43}
44
45 int main ()
46 {
47 cout <"Enter the length of your array:" <endl;
48 int n = 0;
49 cin> n;
50
51 int * pArray = new int [n];
52 cout <"Enter the elements in your array:" <endl;
53 for (int I = 0; I <n; ++ I)
54 {
55 cin> pArray [I];
56}
57
58 int diff = MaxDiff (pArray, n );
59 cout <"The maxDiff in your array is:" <endl;
60 cout <diff <endl;
61
62 delete [] pArray;
63 return 0;
64}
Shows the running result:
Analysis 3: a skillful algorithm that constructs an auxiliary array diff [n-1], where diff [I] = array [I]-array [I + 1]; in this way, diff [I]-diff [j] = (array [I]-array [I + 1]) + (array [I + 1]-array [I + 2]) + ...... + (diff [j]-diff [j + 1]) = array [I]-array [j + 1].
That is to say, the maximum difference between the number pairs of array [] is the sum of the largest sub-arrays of the diff [] array. The sum of the largest sub-array of an array has been discussed in [algorithm 07]. I will not go into details, so we can get the following code:
1 # include <iostream>
2 # include <string>
3 using namespace std;
4
5 int MaxDiff (int array [], int arrayLength)
6 {
7 if (array = NULL | arrayLength <2)
8 return 0;
9
10 // create and initialize a temporary Array
11 int * diff = new int [arrayLength-1];
12 for (int I = 0; I <arrayLength-1; ++ I)
13 diff [I] = array [I]-array [I + 1];
14
15 // returns the sum of the largest subarrays of the temporary array.
16 int currentSum = 0;
17 int largestSum = 0;
18 for (I = 0; I <arrayLength-1; ++ I)
19 {
20 currentSum + = diff [I];
21
22 if (currentSum <0)
23 currentSum = 0;
24
25 if (currentSum> largestSum)
26 largestSum = currentSum;
27}
28
29 if (largestSum = 0)
30 {
31 largestSum = diff [0];
32 for (int I = 0; I <arrayLength-1; ++ I)
33 {
34 if (diff [I]> largestSum)
35 largestSum = diff [I];
36}
37}
38
39 delete [] diff;
40 return largestSum;
41}
42
43 int main ()
44 {
45 cout <"Enter the length of your array:" <endl;
46 int n = 0;
47 cin> n;
48
49 int * pArray = new int [n];
50 cout <"Enter the elements in your array:" <endl;
51 for (int I = 0; I <n; ++ I)
52 {
53 cin> pArray [I];
54}
55
56 int diff = MaxDiff (pArray, n );
57 cout <"The maxDiff in your array is:" <endl;
58 cout <diff <endl;
59
60 delete [] pArray;
61 return 0;
62}
The running result is as follows:
Analysis 4: We can also dynamically consider this issue. We can assume that this array is constantly adding a number to the front of a known array, if we already know the maxdiff of the known array and the minimum value of the known array, how can we solve the maxdiff of the new array, we only need to use the newly added array to subtract the known minimum value and maxdiff to compare it with the known minimum value, take the smaller value, and update the minimum value. What was the initial problem? There is only one number! The minimum value is itself, maxdiff = 0. Based on this idea, we can write the following concise code:
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 int MaxDiff(int array[],int arrayLength)
6 {
7 if(array == NULL || arrayLength < 2)
8 return 0;
9
10 int min = array[arrayLength - 1];
11 int maxdiff = 0;
12 for(int i = arrayLength - 2;i >= 0;--i)
13 {
14 maxdiff = (maxdiff > (array[i] - min)) ? maxdiff:(array[i] - min);
15 min = (min < array[i]) ? min:array[i];
16 }
17 return maxdiff;
18 }
19
20 int main()
21 {
22 cout<<"Enter the length of your array:"<<endl;
23 int n = 0;
24 cin>>n;
25
26 int *pArray = new int[n];
27 cout<<"Enter the elements in your array:"<<endl;
28 for(int i = 0;i < n;++i)
29 {
30 cin>>pArray[i];
31 }
32
33 int diff = MaxDiff(pArray,n);
34 cout<<"The maxDiff in your array is:"<<endl;
35 cout<<diff<<endl;
36
37 delete[] pArray;
38 return 0;
39 }
The running result is as follows:
Efficiency Analysis: The method in analytics 2 adopts the principle of divide and conquer, which breaks down two n/2 questions. Therefore, the time complexity is O (n * logn). However, due to the recursive algorithm, recursive call saves stack parameters and temporary variables. The method in analysis 3 uses a temporary array with the length of N-1 and the space complexity is O (n). According to the analysis in the previous article, the time complexity is O (n ). In analysis 4, the time complexity of the dynamic planning method is O (n), and the storage process of min and maxdiff variables is used. Therefore, the space complexity is O (1 ). Is the most recommended method!
References:
[1] programmer interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/2541117420116135376632/
(2) time complexity of recursive algorithm: http://www.cnblogs.com/wu8685/archive/2010/12/21/1912347.html
Note:
1) All the code environments of this blog are compiled in win7 + VC6. All code has been debugged by the blogger.
2) The blogger python27 has the copyright to this blog post. For the reposted website, please indicate the source at http://www.cnblogs.com/python27 /. You have any suggestions for solving the problem. Please feel free to comment on them.