題目連結
https://www.codewars.com/kata/54521e9ec8e60bc4de000d6c
解法
function maxSequence(arr) {
let maxSum = 0;
for (let i = 0; i < arr.length; i++) {
let currentSum = 0;
for (let j = i; j < arr.length; j++) {
currentSum += arr[j];
maxSum = Math.max(maxSum, currentSum);
}
}
return maxSum;
}
筆記
使用雙重迴圈
比較當前sum和maxSum
最後回傳maxSum
[-2, 1, -3, 4, -1, 2, 1, -5, 4]