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

[ 筆記 ] JavaScript - 02 函式

[ 筆記 ] JavaScript - 02 函式

Day04_Origami學習筆記

Day04_Origami學習筆記

Android switch style 讓switch像是iOS原生的switch

Android switch style 讓switch像是iOS原生的switch


Comments