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; };
当你看清人们的真相,于是你知道了,你可以忍受孤独