sumArray

Written on August 28, 2019

문제

Given an array of numbers, calculate the greatest contiguous(not be off) sum of elements in array. A single array item will count as a contiguous sum.

풀이

var sumArray = function(array) {
  let sums = [];
  const greatestSum = index => {
    let sum = 0;
    for (let i = index; i < array.length; i++) {
      sum += array[i];
      if (!sums[index] || sums[index] < sum) {
        sums[index] = sum;
      }
    }
    if (index === array.length - 1) {
      return;
    }
    greatestSum(index + 1);
  };
  greatestSum(0);
  return Math.max.apply(null, sums);
};

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself