2020/4/4 second software engineering jobs

I. Description of the problem


Topic :

  And the maximum continuous subarray (maximum and sub-segment)

problem:

  Given n integer (possibly negative) consisting of a sequence of a [1], a [2 ], a [n] a [3], ...,, find the sequence, such as a [i] + a [i + 1] + ... + a [j] and the maximum value of the sub-segments. When given definitions are negative integer and sub-segment 0, so defined, the required optimal values: Max {0, a [i ] + a [i + 1] + ... + a [j]} , 1 <= i <= j <= n
  For example, when (a [1], a [ 2], a [3], a [4], a [5], a [6]) = (- 2, 11, -4,13, -5, -2), and the maximum of 20 sub-segment.

 

Second, the solution ideas and methods


  This problem can be solved through violence, dynamic planning method, after the query data and research, I decided to use divide and conquer strategy to solve the problem.

Ideas:

  The sequence is divided into two sections about the middle point of demarcation center = (right-left) / 2 + left;

  Recursive calculation of the maximum field of leftsum left segment;

  Recursive calculation of the maximum right section of the sub-segment and rightsum;

  Referred to the center A . 1 maximum and Sl;

  Referred to the center A n- maximum and S2;

  max{leftsum,rightsum,S1+S2};

 

The largest sub-segment and may appear in three locations:

  A: left subarray 

  B: right sub-arrays 

  A subarray center part through an intermediate of: C

Specific solutions are as follows:

  a. Calculation of maximum and left to center, denoted leftSum. From the center, to the left of each expansion step, and record the current value of S1, if the current and the last time and a large, updated S1, has been expanded to the left to position Left. 

  b. Calculate the center + 1 and to the right of the maximum, denoted rightSum. From the center + 1, each expansion step, and to calculate the current S2, if the current value than the last and large, it would update the value of S2, has been expanded to the right position to Right.

  c. calculate and cross-border. In center to both sides, respectively, and the calculated center. Through the center and continuous values, S1 + S2 value Sum. This is cross-border and.

  Three cases considered above calculations are completed, the final step is to compare the maximum value of the three values, taking the maximum value to it.

 

Three, C ++ source code


This problem has been C ++ source code uploaded to the site: https://github.com/NisannTomo/TOMO/blob/master/2_1%20nMax

 

Fourth, the test case


 

 

Guess you like

Origin www.cnblogs.com/Nisanntomo/p/12630681.html