본문 바로가기
알고리즘 & 자료구조/프로그래머스

구명 보트

by 신재권 2023. 1. 5.
package programmers;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class 구명보트 {

   public static int solution(int[] people, int limit) {
      Arrays.sort(people);
      Deque<Integer> deque = new ArrayDeque<>(50001);

      for (int person : people) {
         deque.add(person);
      }

      int answer = 0;
      while (!deque.isEmpty()) {
         int weight = deque.pollLast();
         if (!deque.isEmpty() && weight + deque.peekFirst() <= limit) {
            deque.pollFirst();
         }
         answer++;
      }

      return answer;
   }

   public static void main(String[] args) {
      System.out.println(solution(new int[] {70, 50, 80, 50}, 100));
      System.out.println(solution(new int[] {70, 80, 50}, 100));
      System.out.println(solution(new int[] {40, 40, 40, 30, 50, 80}, 80));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

예상 대진표  (1) 2023.01.07
N개의 최소공배수  (0) 2023.01.06
영어 끝말잇기  (0) 2023.01.04
짝지어 제거하기  (0) 2023.01.03
카펫  (0) 2023.01.02