Returns the neutron array, the largest value in the two-dimensional array, and the Child array.
Personal information: He is a junior majoring in software engineering at yanda University;
My blog: google searches for "cqs_2012;
My hobbies: I love data structures and algorithms and hope to make my own contribution to algorithm work in the future;
Programming Language: C ++;
Programming Environment: Windows 7 Professional Edition x64;
Programming tools: vs2008;
Drawing tools: office 2010 powerpoint;
Hardware information: 7G-3 Notebook;
Zhenyan
Every time you rush to write a program, you will have a headache when you encounter problems. After the headache, you will be full of gains, and there will always be a return.
Question
Returns the neutron array, the largest value in the two-dimensional array, and the Child array.
Solution
We have already done a one-dimensional array problem. If you have not seen the problem of one-dimensional arrays, please stamp the maximum value of the sum of sub-Arrays
A two-dimensional array is an array of one-dimensional arrays.
My algorithm is to first compress the column, that is, the sum of each column as the element of the new array to form a new array to simplify the problem, and then it becomes a one-dimensional array to find the maximum sub-array problem.
The specific algorithm design is represented in C ++ as follows:
// Obtain the maximum child two-dimensional Array of the Two-dimensional Array int Array: Max_Sum_Sub_Double_Array (int ** data, const unsigned int row, const unsigned int column) {// if (data = NULL | row = 0 | column = 0) {cout <"Max_Max_Sub_Double_Array" <
Column | s> e | e> row) {cout <"exception input Sum_Sub_Column_Double_Array" <
Row | s> e | e> column) {cout <"exception input Sum_Sub_Row_Double_Array" <
= 0) {all_fushu = false; break;} else if (data [I]> data [max]) {max = I ;}} // if all values are negative if (all_fushu = true) {R = L = max + 1; return void (0 );} // if not all negative else {// core algorithm initializes int left_sum = data [0], right_sum = data [length-1]; int left = 0, right = length-1; L = left; R = right; // select the forward direction while (left <right-1) {if (left_sum <right_sum) {if (left_sum <0) {left_sum = 0; L = left + 1;} left ++; left_sum + = data [left];} else {if (right_sum <0) {right_sum = 0; R = right-1;} right --; right_sum + = data [right] ;}// query results // If the left half array is discarded, keep the right array if (left_sum <= 0) {L = right + 1; R ++;} // if you discard the right array, keep the left half of the else if (right_sum <= 0) {L ++; R = left + 1 ;}// else {L ++ is not discarded on both sides; R ++;} return void (0 );}}}