K 次串联后最大子数组之和 JS

 

  Kadnae 解法:

var kConcatenationMaxSum = function (arr, k) {
    let sum = arr[0];
    let newK = k > 2 ? 2 : k;
    let pre = 0;
    let an = 0;
    for (let i = 0; i < newK; i++) {
        if (pre > 0) {
            pre = pre + arr[i % arr.length];
        } else {
            pre = arr[i % arr.length];
        }
        an = an > pre ? an : pre;
        if (i < arr.length) {
            sum += arr[i];
        }
    }
    while (sum > 0 && --k >= 2) {
        an = an + sum;
    }
    return an;
};

 

posted @ 2020-08-04 00:00  牛有肉  阅读(196)  评论(0编辑  收藏  举报