본문 바로가기

코딩테스트 연습/programmers

[프로그래머스] 입국 심사 - JAVA

 

이분 탐색 문제에 대해 익히려고 풀어본 문제.

"최소" 시간을 찾는다고 했으니, start와 end 를 반씩 줄여가면서 해당 시간 안에 모든 사람이 통과할 수 있는 경우를 찾았다!

 

class Solution {
    public long solution(int n, int[] times) {
        long answer = 0;
        
        long start = 0;
        long end = 1000000000L * 1000000000L;
        
        while(end>start){
            long minute = (start+end)/2;
            
            if(check(times, minute, n)){
                end = minute;
            } else {
                start = minute + 1;
            }
        }
        
        
        return start;
    }
    static boolean check(int[] times, long minute, int n){
        long count = 0;
        for(int time: times){
            count += minute/time;
        }
        return count >= n;
    }
}