[프로그래머스]구명보트 (javascript/자바스크립트)

스카치·2023년 4월 17일
0

문제

풀이 1

function solution(people, limit) {
    var answer = 0;
    people.sort((a,b) => a-b)
    let boat = []
    
    while (people.length) {
        boat.push(people.pop())
        answer++
        if (boat[0] + people[0] <= limit) people.shift()
        boat = []            
    }    
    return answer;
}

풀이 2

function solution(people, limit) {
    people.sort(function(a, b){return a-b});
    let i=0
    for(i=i, j=people.length-1; i < j; j--) {
        if( people[i] + people[j] <= limit ) i++;
    }    
    return people.length-i;
}

0개의 댓글