One algorithm question per day -- 6.25 -- Not Defined

Source: Internet
Author: User

Thanks to the platform-one day algorithm question-more progress every day ----

In fact, today I have come up with a few questions that I have made up for. So I just moved my questions and analysis from the ground up.

Original question

Given an array, We can find two consecutive subarrays A and B. The numbers in a are sum (A), and the elements in B are sum (B ). Find such a and B, and the absolute value that satisfies sum (a)-sum (B) is the largest. For example, [2,-1-2, 1,-4, 2, 8] is divided into a = [-1,-2, 1,-4], B = [2, 8]. The maximum value is 16.

Analysis

If you do not have a wealth of experience, you may not feel this question at all. However, you only need to perform layer-by-layer analysis. First, let's take a look at the key points of the question (clarify the meaning of the question, and clarify if there is any unclear question .) :

Child arrays are not intersecting.

The sub-array is continuous. This is a bit redundant, but it is better to emphasize it.

The requirement of the question is that the absolute value of the difference is the largest. We can naturally think of two child arrays that do not overlap. One value is very small and the other value is very large. This ensures that the absolute value of the difference is the largest. How can we find such an array? We start with the condition that we never meet. Look at the example in the question (starting from a simple example, discovering a solution is a common skill in the interview. Remember !) :

0 1 3 4 5 6
2 -1 1 4 2 8

Look at the table above. If two sub-arrays do not want to be handed in, we have six positions as an alternative to Division, between 0 and 1, between 1 and 2, between 2 and 3 ,..., until between 5 and 6. These six positions can divide the array into two parts. We set the length of the array to N, and I divide the data into two parts: [0, I-1] and [I, n-1]. These are the collections contained on both sides. I is 1 to n-1.

For any I, we get two parts [0, I-1] and [I, n-1]. Next, we will find one and the smallest sub-array A, and the largest sub-array B in the two parts. Then the absolute value of the A-B is the maximum difference between the two arrays that meet the condition under the I division. For all I, the and B values of the absolute values are what we need to find.

Now, we can find the Division at I, and the maximum and minimum sub-arrays. Here, we will use the idea of dynamic planning that we shared a few days ago. Have you read and analyzed the article. I believe it will surely bring a lot of inspiration to everyone. Back to this question, we separately consider the given array, the Child array with the greatest sum, and the smallest child array.

First, analyze and the largest sub-array. This problem is a classic problem, but what we need to deal with here is to obtain the maximum continuous sub-array on the left side of each I. For the following analysis, we assume that the array is X, and max_until [I] indicates the maximum sum of consecutive subarrays ending with I. What is the relationship between max_until [I] And max_until [I-1?

  1. For example, X [I] + max_until [I-1]> max_until [I-1] and X [I] + max_until [I-1]> X [I]. Then X [I] should be added to the continuous subarray, max_until [I] = max_until [I-1] + X [I].

  2. Otherwise, max_until [I] = x [I] has only one element in the continuous subarray.

But yes, what we want is not a subarray ending with I. Although this is the case in the example, what we want is the largest and all consecutive subarrays before I. It does not necessarily include I. What should we do? We then open the subarray max_left [I] to indicate the maximum value of the continuous subarray in [0, I. How can we obtain this value? When we traverse the array and obtain max_until [I], max_left [I] only needs to obtain the maximum value in max_until [I] and the previously saved maximum value. That is, the max_until array and the max_left array can be obtained after a traversal. Similarly, the min_until and min_left Arrays can be obtained.

This is the left half of the Processing Division. What about the right half?

The idea for the right part is the same, but we need to traverse the array from right to left when traversing.

The process for summarizing the entire method is as follows:

  1. Traverse the array from left to right, calculate the max_left and min_left arrays, and calculate the O (n) time complexity.

  2. Traverses the array from the right to the left, computes the max_right and min_right arrays, and the O (n) time complexity.

  3. Then, for each I, I starts from 1 to n-1, max_left [I-1]-min_right [I], max_right [I]-min_left [I-1] is calculated. Select the largest absolute value.

The total space complexity of the method is O (n), and the time complexity is O (n ).

 

// The pasted code comes from ----- sysu_arui

I have been busy over the past few days. I have no time for the final exam. Today, the Win8 system network can be normally connected to webpages, and various QQ games cannot be played ..........

Prepare to reinstall win7win8

Too annoying

  1 #include <assert.h>  2 #include <stdlib.h>  3 #include <limits.h>  4 #define N 100  5   6 int max(int num1, int num2)  7 {  8 return num1 > num2 ? num1 : num2;  9 } 10  11 int min(int num1, int num2) 12 { 13 return num1 < num2 ? num1 : num2; 14 } 15  16 int max_difference(int *array, int n) 17 { 18 int *max_left = (int*)malloc(sizeof(int) * n); 19 int *min_left = (int*)malloc(sizeof(int) * n); 20 int *max_right = (int*)malloc(sizeof(int) * n); 21 int *min_right = (int*)malloc(sizeof(int) * n); 22 //initialize 23 int max_left_until = array[0]; 24 int min_left_until = array[0]; 25 max_left[0] = array[0]; 26 min_left[0] = array[0]; 27 //calculate max_left and min_left 28 for(int i=1; i<n; i++) 29 { 30 if(max_left_until > 0) 31 max_left_until += array[i]; 32 else 33 max_left_until = array[i]; 34 if(min_left_until < 0) 35 min_left_until += array[i]; 36 else 37 min_left_until = array[i]; 38 max_left[i] = max(max_left[i-1], max_left_until); 39 min_left[i] = min(min_left[i-1], min_left_until); 40 } 41 //print max_left and min_left 42 printf("\n max_left = "); 43 for(int i=0; i<n; i++) 44 { 45 printf("%d ", max_left[i]); 46 } 47 printf("\n min_left = "); 48 for(int i=0; i<n; i++) 49 { 50 printf("%d ", min_left[i]); 51 } 52 //initialize 53 int max_right_until = array[n-1]; 54 int min_right_until = array[n-1]; 55 max_right[n-1] = array[n-1]; 56 min_right[n-1] = array[n-1]; 57 //calculate max_right and min_right 58 for(int i=n-2; i>=0; --i) 59 { 60 if(max_right_until > 0) 61 max_right_until += array[i]; 62 else 63 max_right_until = array[i]; 64 if(min_right_until < 0) 65 min_right_until += array[i]; 66 else 67 min_right_until = array[i]; 68 max_right[i] = max(max_right[i+1], max_right_until); 69 min_right[i] = min(min_right[i+1], min_right_until); 70 } 71 //print max_right and min_right 72 printf("\n max_right = "); 73 for(int i=0; i<n; i++) 74 { 75 printf("%d ", max_right[i]); 76 } 77 printf("\n min_right = "); 78 for(int i=0; i<n; i++) 79 { 80 printf("%d ", min_right[i]); 81 } 82 int max_diff = INT_MIN; 83 for(int i=1; i<n; ++i) 84 { 85 int temp = max(max_left[i-1] - min_right[i], 86 max_right[i] - min_left[i-1]); 87 if(temp > max_diff) 88 max_diff = temp; 89 } 90 free(max_left); 91 free(min_left); 92 free(max_right); 93 free(min_right); 94 return max_diff; 95 } 96  97 int main(int argc, char **argv) 98 { 99 //a = 7 2 -1 -2 1 -4 2 8100 int a[N];101 int n; //actual array size102 while(scanf("%d", &n) != EOF)103 {104 for(int i=0; i < n; ++i)105 {106 scanf("%d",&a[i]);107 }108 printf("\n max_difference = %d\n", max_difference(a, n));109 }    110 return 0;111 } 
View code

 

Today:

Sweet hand-in-hand is short-lived

The pain of breaking up is eternal.

From ------ master of breakup

Personal opinions should not be

Dare not harm the girl

 

 

 

 

 

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.