[Algorithm] 40 week(10.31 ~ 11.04) 2/3

Dev_min·2022년 11월 1일
0

algorithm

목록 보기
130/157

구명보트

function solution(people, limit) {
  let count = 0;
  people.sort((a,b) => a-b)
  let left = 0
  let right = people.length-1;
  
  while(left <= right) {
      const sum = left < right ? people[left] + people[right] : people[right]
    
      if(sum > limit) {
          right--;
      } else {
          left++;
          right--;
      }
      
      count++;
  }
    
  return count;
}
profile
TIL record

0개의 댓글