[백준/이분탐색] 3079번 입국심사 (JAVA)

Jiwoo Kim·2021년 5월 1일
0

알고리즘 정복하기

목록 보기
77/85
post-thumbnail

문제

프로그래머스의 입국심사 문제와 같은 문제다.
자세한 풀이는 이전 포스팅에 적어 놓았고, 아래 코드는 백준 형식에 맞춰 살짝만 바꿔 작성해본 코드다.


코드

import java.io.*;
import java.util.Arrays;

public class Main {

    private static int n;
    private static int peopleCount;
    private static int[] times;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        parseInput(br);
        bw.append(String.valueOf(calcMinTime()));

        br.close();
        bw.close();
    }

    private static long calcMinTime() {
        long left = 1;
        long right = (long) peopleCount * times[times.length - 1];

        long answer = 0;
        while (left <= right) {
            long mid = (left + right) / 2;
            long maxPeopleCount = getPeopleCount(mid, times);

            if (maxPeopleCount < peopleCount) {
                left = mid + 1;
            } else {
                right = mid - 1;
                answer = mid;
            }
        }
        return answer;
    }

    private static long getPeopleCount(long timeLimit, int[] times) {
        long count = 0;
        for (int time : times) count += timeLimit / time;
        return count;
    }

    private static void parseInput(BufferedReader br) throws IOException {
        String[] line = br.readLine().split(" ");
        n = Integer.parseInt(line[0]);
        peopleCount = Integer.parseInt(line[1]);

        times = new int[n];
        for (int i = 0; i < n; i++)
            times[i] = Integer.parseInt(br.readLine());
        Arrays.sort(times);
    }
}

0개의 댓글