Learning record - Array algorithm problem: the largest sub-array

Excerpt现代 JavaScript 教程

Casual working

Input is an array of numbers, for example arr = [1, -2, 3, 4, -9, 6].

Mission: to find contiguous arrsub-array, it all inside and maximum.

Write function getMaxSubSum(arr), use it to find and return the maximum and.

E.g:

getMaxSubSum([-1, 2, 3, -9]) = 5 (高亮项的加和)
getMaxSubSum([2, -1, 2, 3, -9]) = 6
getMaxSubSum([-1, 2, 3, -9, 11]) = 11
getMaxSubSum([-2, -1, 1, 2]) = 3
getMaxSubSum([100, -9, 2, -3, 5]) = 100
getMaxSubSum([1, 2, 3]) = 6 (所有项的和)
复制代码

solution

Slow Solutions

We can calculate all possible subsets of and.

The easiest way is to get each element is then calculated from its beginning and all sub-arrays.

With [-1, 2, 3, -9, 11]an example:

// 从 -1 开始:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11

// 从 2 开始:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11

// 从 3 开始:
3
3 + (-9)
3 + (-9) + 11

// 从 -9 开始:
-9
-9 + 11

// 从 -11 开始:
-11
复制代码

Such code is actually write out a nested loops: the external loop through the array of all the elements, then the internal cycle after the current element is calculated from the set of all sub-array and.

function getMaxSubSum(arr) {
  let maxSum = 0; // 如果没有取到任何元素,就返回 0

  for (let i = 0; i < arr.length; i++) {
    let sumFixedStart = 0;
    for (let j = i; j < arr.length; j++) {
      sumFixedStart += arr[j];
      maxSum = Math.max(maxSum, sumFixedStart);
    }
  }

  return maxSum;
}

alert( getMaxSubSum([-1, 2, 3, -9]) ); // 5
alert( getMaxSubSum([-1, 2, 3, -9, 11]) ); // 11
alert( getMaxSubSum([-2, -1, 1, 2]) ); // 3
alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
复制代码

The time complexity of this scheme is O (n2). That is, if we increase two times the size of the array, then the running time will be extended four times.

For large arrays (1000,10000 or more items) This algorithm can cause serious time consuming.

Fast solutions

Let us traverse the array, the current local elements and stored as a variable s. If you sbecome negative at some point, it is reallocated s=0. All sthe maximum value is the answer.

If the text is not a good understanding of description, the following code directly see it, really short:

function getMaxSubSum(arr) {
  let maxSum = 0;
  let partialSum = 0;

  for (let item of arr) { // arr 中的每个 item
    partialSum += item; // 将其添加到 partialSum
    maxSum = Math.max(maxSum, partialSum); // 记住最大值
    if (partialSum < 0) partialSum = 0; // 如果是负数就置为 0
  }

  return maxSum;
}

alert( getMaxSubSum([-1, 2, 3, -9]) ); // 5
alert( getMaxSubSum([-1, 2, 3, -9, 11]) ); // 11
alert( getMaxSubSum([-2, -1, 1, 2]) ); // 3
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([-1, -2, -3]) ); // 0
复制代码

The algorithm only needs to traverse an array, the time complexity is O (n).

A minimal array

Similarly

function getMaxSubSum(arr) {
  let maxSum = 0;
  let partialSum = 0;

  for (let item of arr) { // arr 中的每个 item
    partialSum += item; // 将其添加到 partialSum
    maxSum = Math.min(maxSum, partialSum); // 记住最小值
    if (partialSum > 0) partialSum = 0; // 如果是正数就置为 0
  }

  return maxSum;
}

alert( getMaxSubSum([-1, 2, 3, -9]) ); // -9
复制代码

Reproduced in: https: //juejin.im/post/5d07b5d36fb9a07ef16183d9

Guess you like

Origin blog.csdn.net/weixin_34384915/article/details/93177684