구명보트

hyuri·2023년 10월 18일
0

코딩테스트 연습

목록 보기
66/70

내가 작성한 답

function solution(people, limit) {
    let answer = 0;
    people.sort((a,b) => a - b);
    
    let mix = 0;
    let max = people.length -1;
    
    
    while(mix <= max){
        if (people[mix] + people[max] <= limit){
            mix++;
        }
        max--;
        answer++;
    }
    return answer;
}

다른 답

function solution(people, limit) {
    people.sort(function(a, b){return a-b});
    for(var i=0, j=people.length-1; i < j; j--) {
        if( people[i] + people[j] <= limit ) i++;
    }    
    return people.length-i;
}
// limit에 최대한 가깝게
function solution(people, limit) {
  let biggest = 0,
    count = 0,
    i = 0;
  people.sort((a, b) => a - b);
  while (people.length > 0) {
    biggest = people.pop();
    i = 0;
    while (people[i] <= limit - biggest) i++;
    if (i) people.splice(i - 1, 1);
    count++;
  }
  return count;
}
function solution(people, limit) {
    people.sort((a, b) => b - a);
    let count = 0;
    while(people.length) {
        if(people[0] + people[people.length - 1] <= limit) {
            people.shift();
            people.pop();
        }else {
            people.shift();
        }
        count++;
    }
    return count
}

해석

shift는 남은 인덱스를 한 칸씩 앞으로 당겨와야해서
profile
개발자가 되고 싶은 지망생

0개의 댓글