[Algorithm] 37 week(10.10 ~ 10.16) 1/3

Dev_min·2022년 10월 10일
0

algorithm

목록 보기
120/157
  1. Trapping Rain Water
var trap = function(height) {
    if(height.length === 0) return 0;

    let volume = 0;
    let left = 0;
    let right = height.length - 1;
    let leftMax = height[left];
    let rightMax = height[right];

    while(left <= right){
        leftMax = Math.max(height[left], leftMax);
        rightMax = Math.max(height[right], rightMax);

        if(leftMax <= rightMax){
            volume += leftMax - height[left];
            left += 1;
        } else {
            volume += rightMax - height[right];
            right -= 1;
        }
    }

    return volume;
};
profile
TIL record

0개의 댓글

관련 채용 정보