Application examples - the largest sub-columns and issues

The largest sub-columns and issues

Given \ (N \) integers sequence \ ({A_1, A_2, \ DOTS, A_N} \) , find the function \ (f (i, j) = max \ {0, \ sum_ {k = I} ^ jA_K \} \) maximum.

The sequence of multiple sub-columns, we need to find out the sub-columns and the largest sub-columns.

Brute force algorithm 1-

/* c语言实现 */

int MaxSubseqSum1(int A[], int N)
{ int ThisSum, MaxSum = 0;
  int i, j, j;
  for(i=0; i<N; i++){  /* i是子列左端位置 */
    for (j=i; j<N; j++){  /* i是子列右端位置 */
      ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和 */
      for(k=i; k<=j; k++)
        ThisSum += A[k];
      if(ThisSum > MaxSum)  /* 如果刚得到的这个子列和更大 */
        MaxSum = ThisSum;  /* 则更新结果 */
   }  /* j循环结束 */
 }  /* i循环结束 */
 return MaxSum;
}
# python语言实现

def max_subseq_sum1(arr: list, n: int):
  max_sum = 0
  for i in range(n):
    for j in range(i, n):
      this_sum = 0
      for k in range(i, j):
        this_sum += arr[k]
      if this_sum > max_sum:
        max_sum = this_sum

Time Complexity:
\ [T (n-) = O (N ^. 3) \]
When we know \ (ij of \) and after the addition is not necessary to start from scratch, for the k cycle is superfluous.

2- appropriate optimization algorithm

/* c语言实现 */

int MaxSubseqSum2(int A[], int N)
{ int ThisSum, MaxSum = 0;
  int i, j, j;
  for(i=0; i<N; i++){  /* i是子列左端位置 */
    ThisSum = 0; /* ThisSum是从A[i]到A[j]的子列和 */
    for (j=i; j<N; j++){  /* i是子列右端位置 */
      ThisSum += A[k];
      /* 对于相同的i,不同的j,只要在j-1次循环的基础上累加1项即可 */
      if(ThisSum > MaxSum)  /* 如果刚得到的这个子列和更大 */
        MaxSum = ThisSum;  /* 则更新结果 */
   }  /* j循环结束 */
 }  /* i循环结束 */
 return MaxSum;
}
# python语言实现

def max_subseq_sum2(arr: list, n: int):
  max_sum = 0
  for i in range(n):
    this_sum = 0
    for j in range(i, n):
      this_sum += arr[k]
      if this_sum > max_sum:
        max_sum = this_sum

Time Complexity:
\ [T (the n-) = O (N ^ 2) \]
a professional program, designed a \ (O (N ^ 2) \) algorithm, should instinctively think if he can improve as \ (O (Nlog_N) \) algorithm.

Divide and conquer algorithm 3-

From the first intermediate array into two, recursive problem left half , resulting the left column and the sub-maximum; recursive solve the problem of the right half , the sub-maximum and rightmost column; settlement across the middle of the largest sub- column and taken out from the maximum sub-column and three, is the solution.

Time Complexity:
\ [\} the begin align = left {T (n-) & 2T = (N / 2) + cN, \, \ Quad {T} (. 1) O = (. 1) & \\ = \ Note text $ { T (n) $ and $ T (N / 2) $ conversion relation} \\ & = 2 [2T ( N / 2 ^ 2) + cN / 2] + cN \\ & = 2 ^ kO (1) + ckN \ quad \ text {where $ N / 2 ^ k = 1 $, i.e. $ k = log_2N $} \\ & = O (NlogN) \ end {align} \]

Online processing algorithm 4-

Because of the continuous sub-columns and negative numbers , plus the number back behind the figures it will only make more and more, so you can make ahead and column sub-zero.

"Online" is meant to be on each subsequent data processed immediately suspend any input in one place, the current solution can give the correct algorithm.

/* c语言实现 */

int MaxSubseqSum4(int A[], int N)
{ int ThisSum, MaxSum;
  int i;
  ThisSum = MaxSum = 0;
  for(i=0; i<N; i++){
    ThisSum += A[i]; /* 向右累加 */
    if(ThisSum > MaxSum)
      MaxSum = ThisSUm; /* 发现更大和则更新当前结果 */
    else if(ThisSum < 0) /* 如果当前子列和为负 */
      ThisSum = 0; /* 则不可能使后面的部分和增大,抛弃之 */
 }
 return MaxSum;
}
# python语言实现

def max_subseq_sum4(arr: list, n: int):
  this_sum = max_sum = 0
  for i in range(n):
    this_sum += arr[i]
    if this_sum > max_sum:
      max_sum = this_sum
    elif this_sum < 0:
      this_sum = 0

Time Complexity:
\ [T (N) = O (N) \]
Since the time complexity of fast, for others it is understood that it is difficult .

Running time compare

Guess you like

Origin www.cnblogs.com/nickchen121/p/11408770.html