Coding Interview

알고리즘 유형을 알아보자

milliwonkim 2022. 10. 1. 17:51
반응형
SMALL

카데인의 알고리즘

각 수를 더했을 때 가장 큰수가 나오는 연속된 부분을 찾는 알고리즘(maxSubArray)

const maxSubArray = nums => {
    let max = nums[0];
    let current = Math.max(max, 0);
    
    for (let i = 1; i < nums.length; i += 1) {
        current += nums[i];
        max = Math.max(max, current);
        current = Math.max(current, 0);
    }
    
    return max;
};

 

반응형
LIST