The maximum value of the difference between the pairs && the largest of the sub-arrays

Source: Internet
Author: User
Tags diff

Question 1: In the array, the number minus his right number gets a number pair difference, and the maximum value of the difference between all the pairs is obtained. For example, in array {2.4.1.16.7.5.11.9}, the maximum value of the difference between pairs is 11, which is the result of 16 minus 5.

Question 2: Given a sequence of n elements, the element has a positive negative, find and the smallest set of adjacent books, both given a[n], yes a[i]+a[i+1]+...+a[j] and minimum.

Let's look at the first topic:

If you traverse from the beginning, traverse to a position, start at this position to iterate to the end of the array, look at the value of the number of pairs of this element, and then the maximum value of the difference between the current number pairs, O (n^2) event complexity.

Solution Two: The biggest and the problem of converting to solving sub-arrays

Next, we introduce a more ingenious solution. If you enter a length ofNthe arraynumbers, we first construct a length ofn-1the auxiliary arraydiff, andDiff[i]equalsNumbers[i]-numbers[i+1](0<=i<n-1). If we are from an arraydiffthe article inInumber has been accumulated to the firstJa number (J > I), i.e.Diff[i] + diff[i+1] + ... + diff[j] = (numbers[i]-numbers[i+1]) + (numbers[i + 1]-numbers[i+2]) + ... + (numbers[j]–nu Mbers[j + 1]) = numbers[i]–numbers[j + 1].

In this analysis, we find that the difference between the largest number pairs in the original array (i.e., numbers[i]–numbers[j + 1]) is actually the sum of the largest contiguous subarray in the auxiliary array diff .

int Maxdiff_solution2 (int numbers[],unsigned length)

{

if (numbers = = NULL | | length < 2)

return 0;

int* diff = newint[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;

}

Solution Three: Dynamic Programming method

Since we can change the difference between the maximum number of pairs and the maximal sum of the subarray, and the maximal sum of the sub-arrays can be solved by dynamic programming, can we solve them directly by dynamic programming? Below we try to use the dynamic programming method to directly find the maximum value of the difference between the numbers.

We define Diff[i] to be the maximum of all pairs of numbers that are subtracted from the first number in the array. i.e. for any H(h < i),Diff[i]≥number[h]-number[i]. The maximum value of Diff[i](0≤i<n) is the difference between the maximum number pairs of the entire array.

Let's say we've got it.Diff[i], How should we findDiff[i+1]It? ForDiff[i], there must be ah(H < i) to meetNumber[h]MinusNumber[i]The difference is the largest, namelyNumber[h]Should beNumber[i]The maximum value of all previous numbers. When we askDiff[i+1]When we need to find the firsti+1The maximum value before a number. Thei+1The maximum value before a number can be: the maximum value may beIThe maximum value before the number, it is possible that the maximum value is the firstIA number. Thei+1The maximum value before the number must be the larger of the two. We just have to take the firsti+1The maximum value before the number minusNumber[i+1], you get theDiff[i+1]。

int Maxdiff_solution3 (int numbers[],unsigned length)

{

if (numbers = = NULL | | length < 2)

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;

}

In the preceding code,Max represents the maximum value before the first- i number, while Currentdiff represents diff[i](0≤i<n),diff[ The maximum value of I] is maxdiffin the code.


The above is the method to solve the first problem, as for the second problem, the smallest and the continuous array is obtained. If we set an auxiliary array, this array is b[n]

B[0] = a[0], b[1] = a[0]+a[1] b[2] = a[0]+a[1]+a[2] b[3] = a[0]+a[1]+a[2]+a[3] b[i] = a[0]+a[1]+...+a[i] b[n] = a[0]+a[ 1]+.....+a[n]

Then a[i]+a[i+1]+......+a[j] = b[j]-b[i-1] In other words, the smallest sum of a continuous sequence is equal to the minimum value of the difference between the number pairs in the B[n] array.

This is equivalent to the first question, where s[] is an auxiliary array

J = 1; I = 0; max = 0;for (i=1; i<n; i++) {  if (S[i]-s[max] < s[j]-s[i]) {    i = max;  J = i;  }  
The idea of this algorithm is: S[i] is definitely s[j] before the largest number, S[j] is also s[i] after the smallest number, so keep the largest number S[max], with the current number of s[i] to reduce it (certainly less than s[i]-s[i], see if it is less than s[j]-s [i], if so, then S[i]-s[max] is the smallest difference pair to I. Scan S[n] To get results, O (n)!

This is just the subscript that uses the largest element!




The maximum value of the difference between the pairs && the largest of the sub-arrays

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.