javascript - 我的循环运行了两次,没有给出正确的答案任何解决方案

标签 javascript algorithm list data-structures

<分区>

** 最大和子数组问题在于找到最大和

of a contiguous subsequence in an array or list of integers: maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]) // should be 6: [4, -1, 2, 1] Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

Empty list is considered to have zero greatest sum. Note that the

空列表或数组也是有效的子列表/子数组。**

var maxSequence = function(arr) {
    // ...
    let max = 0;
    const sorted = arr.sort((a, b) => {
        return a - b;
    });
    if (arr.length == 0) {
        return 0;
    } else if (sorted[sorted.length - 1] >= 0 && sorted[0] >= 0) {
        let max = 0;
        for (let i = 0; i < sorted.length; i++) {
            max += sorted[i];
        }
        return max;
    } else if (sorted[0] < 0 && sorted[sorted.length - 1] < 0) {
        console.log(0);
    } else {
        let halfLength = Math.floor(arr.length / 2);
        let pivot = 0;
        let sequence = 0;
        let next = 1;
        while (pivot <= arr.length - 1) {
            if (arr[next] <= halfLength) {
                sequence += arr[next];
                next += 1;
            } else {
                sequence = 0;
                halfLength += 1;
                pivot += 1;
                next = pivot + 1;
            }

            if (pivot == arr.length - 2) {
                sequence += arr[next];
                next += 1;
                break;
            }

            if (sequence >= max) {
                max = sequence;
            }
        }
        console.log("the answer", max);
    }
};

maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]); //, 6)

**The code return 12 instead of 6 any solution i have been trying for an hour now **

最佳答案

您可以针对所有组合对其进行测试,看看哪一个会给您最好的分数。

function maxSequence(data) {
  let result = {
    value: null,
    seq: null
  }

  let check = {
    pos: true,
    neg: true
  }

  data.forEach(e => {
    if (e > 0) check.neg = false;
    if (e < 0) check.pos = false;
  })

  if (check.pos) {
    return sum(data)
  } else if (check.neg) {
    return 0;
  }

  function sum(seq) {
    return seq.reduce((r, e) => r + e, 0)
  }

  for (let i = 0; i < data.length; i++) {
    for (let j = i; j < data.length; j++) {
      const seq = data.slice(i, j + 1);
      const seqSum = sum(seq);

      if (result.value === null || seqSum > result.value) {
        result.value = seqSum;
        result.seq = seq;
      }
    }
  }

  return result;
}

console.log(maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]))
console.log(maxSequence([1, 5, 9, 1]))
console.log(maxSequence([-1, -2, -3, -4]))

关于javascript - 我的循环运行了两次,没有给出正确的答案任何解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59271775/

相关文章:

algorithm - 返回最大可能矩形 block 中可用空间的算法是什么?

python - 根据列表将列表包装成自定义字符串

c - 我怎么知道我是否已经成功释放了整个单链表数组?

Python:变长元组

javascript - “Thinking in AngularJS”,如果我有jQuery背景?

javascript - 之前使用 load 时 jQuery 追加不起作用

sql - 使用 SQL 查询进行轮盘赌选择

javascript - HTML5 中的音频数据流

JavaScript 通过滚动添加一次类

algorithm - 寻找二叉树的边界