DAY36:Maximum subarray sum


Posted by birdbirdmurmur on 2023-08-18

題目連結

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]


#javascript #Codewars







Related Posts

[ 筆記 ] HTML - tag 基礎標籤

[ 筆記 ] HTML - tag 基礎標籤

Lidemy 綜合能力測驗破關&心得

Lidemy 綜合能力測驗破關&心得

Day 81

Day 81


Comments