[Classic Interview Questions] [Netease] array segmentation, question Array

Source: Internet
Author: User

[Classic Interview Questions] [Netease] array segmentation, question Array
[Question]

Any 2N positive integers are selected from them to minimize the difference between the N integers and the sum of the N integers.

[Source]

Netease

[Analysis]

Assume that the SUM of all elements in array A [1 .. 2N] is sum. Simulates the dynamic programming strategy to solve the 0-1 knapsack problem.

Find N elements from the 2N number. There are three possibilities: greater than Sum/2, less than Sum/2, and equal to Sum/2. The value greater than Sum/2 is no different from the value smaller than or equal to Sum/2, so we can only consider the case where the value is less than or equal to Sum/2.

S (k, I) indicates the sum of any I elements in the first k elements. Apparently:

S (k, 1) = {A [I] | 1 <= I <= k}

S (k, k) = {A [1] + A [2] +... + A [k]}

S (k, I) = S (K-1, I) U {A [k] + x | x belongs to S (K-1, I-1 )}

Calculate according to this recursive formula, and finally find the SUM closest to SUM/2 in the Set S (2N, N). This is the answer.

[Analysis 1]

State transition equation:


S [I-1] [j] [k] indicates that j elements are selected from the previous I-1 so that their sum does not exceed but is closest to k;

        S [I-1] [J-1] [k-A [I] selecting I-1 elements from the previous J-1 to make them and not greater than but closest to k-A [I], in this way, when A [I] is added, that is, the I-th element becomes the sum closest to k when the I-th element is selected.

The first and second cases are complete and mutually exclusive. Therefore, you must use the maximum value of the two as the value of F [I] [j] [k.

You can set a 3D array Path [] [] [] to record the trajectory of the selected element. Based on the obtained Path [] [] [], we can deduce it from S [2N] [N] [Sum/2] to S [0] [0] [0 ]. print the elements corresponding to the trajectory.

The time complexity of this algorithm is O (n ^ 2 * sum), and the space complexity is O (n ^ 2 * sum ).

Code 1]

/********************************** Date: question: array Division * Source: Netease * blog: * *********************************/# include <iostream> # include <cstring> # include <algorithm> using namespace std; // simulates the dynamic planning policy for solving the 0-1 knapsack problem int MinSum (int num [], int n) {if (n <= 0) {return 0 ;} // if int sum = 0; int size = 2 * n; // the sum of all number for (int I = 1; I <= size; ++ I) {sum + = num [I];} // for int dp [size + 1] [n + 1] [sum/2 + 1]; // select the numeric path int path [size + 1] [n + 1] [sum/2 + 1]; memset (dp, 0, sizeof (dp )); memset (path, 0, sizeof (path); // uses dp (I, j, k) to represent the j elements in the previous I element, so that the Sum is not greater than k and closest to k, j <= min (I, n), k <= Sum/2 // state transition equation: // dp (I, j, k) = max {dp (I-1, J-1, k-num [I]) + num [I], dp (I-1, j, k)} // dp (2N, n, SUM/2 + 1) is the solution of the question. // The first I element for (int I = 1; I <= size; ++ I) {// you can select the j element int count = min (I, n ); for (int j = 1; j <= count; ++ j) {// make the sum of the for (int k = 1; k <= sum/2; ++ k) {// dp [I] [j] [k] = max (dp [I-1] [j] [k], dp [I-1] [J-1] [k-num [I] + num [I]); // whether to select the I-th element dp [I] [j] [k] = dp [I-1] [j] [k]; if (k> = num [I] & dp [I-1] [j] [k] <dp [I-1] [J-1] [k-num [I] + num [i]) {dp [I] [j] [k] = dp [I-1] [J-1] [k-num [I] + num [I]; path [I] [j] [k] = 1 ;} // if} // for // print the selected path int I = 2 * n, j = n, k = sum/2; while (I> 0 & j> 0, k> 0) {if (path [I] [j] [k] = 1) {cout <num [I] <"; -- j; k-= num [I];} // if -- I ;}// while cout <endl; int sum1 = dp [size] [n] [sum/2]; int sum2 = sum-dp [size] [n] [sum/2]; cout <"Sum1->" <sum1 <"sum2->" <sum2 <endl; return abs (sum1-sum2);} int main () {// int A [] = {, 17}; // int A [] =, 10}; int A [] = {, 10}; int n = 2; cout <"Minimum deviation->" <MinSum (A, n) <endl; return 0 ;}


[Analysis 2]

From the code above, we can see that S [I] [j] [k] is only related to S [I-1] [] [], which can also be reflected in the state equation. We can find a way to remove this fixed relationship. This saves space.

We can use a two-dimensional array instead of a three-dimensional array to reduce space complexity.

But how can we replace it? Because S [I] [j] [k] is only related to S [I-1] [] [], I can be omitted.

In addition, we should use two-dimensional arrays to traverse the "j" dimension of S [I] [j] [k] in reverse order.

Why? Because only in this way can we ensure that the S [I-1] [j] [k] used in the calculation of S [I] [j] [] and S [I-1] [J-1] [] is true the value of the state of I-1, if the forward direction goes through the calendar, when S [] [j] [] is calculated, S [] [J-1] [] has changed, then the calculation result is wrong.

Code 2]

/********************************** Date: question: array Division * Source: Netease * blog: * *********************************/# include <iostream> # include <cstring> # include <algorithm> using namespace std; int MinSum (int num [], int n) {if (n <= 0) {return 0 ;}// if int sum = 0; int size = 2 * n; // the sum of all number for (int I = 1; I <= size; ++ I) {sum + = num [I];} // for int dp [n + 1] [sum/2 + 1]; // select the numeric path int path [size + 1] [n + 1] [sum/2 + 1]; memset (dp, 0, sizeof (dp )); memset (path, 0, sizeof (path); // for (int I = 1; I <= size; ++ I) {// select the j element int count = min (I, n); for (int j = count; j> = 1; -- j) {// make it and not more than k and the closest to k for (int k = num [I-1]; k <= sum/2; + + k) {if (dp [j] [k] <dp [J-1] [k-num [I-1] + num [I-1]) {dp [j] [k] = dp [J-1] [k-num [I-1] + num [I-1]; path [I] [j] [k] = 1 ;} // if} // for // print the selected path int I = 2 * n, j = n, k = sum/2; while (I> 0 & j> 0 & k> 0) {if (path [I] [j] [k] = 1) {cout <num [I] <"; -- j; k-= num [I];} // if -- I ;}// while cout <endl; int sum1 = dp [n] [sum/2]; int sum2 = sum-dp [n] [sum/2]; cout <"Sum1->" <sum1 <"sum2->" <sum2 <endl; return abs (sum1-sum2);} int main () {int A [] = {, 17}; // int A [] =, 10}; // int A [] = {, 10}; int n = 5; cout <"Minimum deviation->" <MinSum (A, n) <endl; return 0 ;}

[Idea 3]

/* ----------------------------------- * Date: 2015-02-01 * Author: SJF0115 * Subject: array Division * Source: Netease * blog: ---------------------------------- */# include <iostream> # include <cstring> # include <algorithm> using namespace std; int MinSum (int num [], int n) {if (n <= 0) {return 0;} // if int sum = 0; int size = 2 * n; // the sum of all number for (int I = 1; I <= size; ++ I) {sum + = num [I];} /// for // isOK [I] [v] indicates whether the number of I can be found, so that the sum of them is equal to v int isOK [n + 1] [sum/2 + 1]; int path [size + 1] [n + 1] [sum/2 + 1]; // memset (isOK, 0, sizeof (isOK) is invalid )); memset (path, 0, sizeof (path); // pay attention to initialization // yes, take 0 items, total 0, valid isOK [0] [0] = 1; for (int I = 1; I <= size; ++ I) {int count = min (I, n ); for (int j = 1; j <= count; ++ j) {// from large to small, one-dimensional for (int k = sum/2; k> = num [I]; -- k) {if (isOK [J-1] [k-num [I]) {isOK [j] [k] = 1; path [I] [j] [k] = 1 ;} // if} // for // print the selected path int I = 2 * n, j = n, k = sum/2; while (I> 0 & j> 0 & k> 0) {if (path [I] [j] [k] = 1) {cout <num [I] <"; -- j; k-= num [I];} // if -- I ;}// while cout <endl; // int minNum; for (int k = sum/2; k> = 0; -- k) {if (isOK [n] [k]) {minNum = k; break;} // if} // for int sum1 = minNum; int sum2 = sum-minNum; cout <"Sum1->" <sum1 <"sum2->" <sum2 <endl; return abs (sum1-sum2);} int main () {// int A [] = {, 17}; // int A [] =, 10}; int A [] = {, 10}; int n = 2; cout <"Minimum deviation->" <MinSum (A, n) <endl; return 0 ;}





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.