https://school.programmers.co.kr/learn/courses/30/lessons/43238
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
저는 이분탐색을 통해 문제를 해결하였습니다.
해당 시간안에 해당 인원을 모두 검사할 수 있는지를 판단하여 검사할 수 있다면, 결과값을 해당 값으로 초기화하고, 끝값을 늘리고, 그렇지 않다면, 시작값을 올려가면서 문제를 해결하였습니다.
class Solution {
public long solution(int n, int[] times) {
long answer = binarySearch(n,times);
return answer;
}
private static final long START = 1L;
private static final long END = 1_000_000_000L * 1_000_000_000L;
private static long binarySearch(int n, int[] times){
long start = START;
long end = END;
long result = 0;
while(start <= end){
long mid = (start + end) / 2;
if(check(mid,times,n)){
result = mid;
end = mid - 1;
}else{
start = mid + 1;
}
}
return result;
}
private static boolean check(long mid, int[] times, int n){
long sum = 0;
for(int time : times){
sum += mid/time;
}
if(sum >= n){
return true;
}else{
return false;
}
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 단속카메라 (JAVA) (0) | 2023.08.17 |
---|---|
프로그래머스 양과 늑대 (JAVA) (0) | 2023.08.16 |
프로그래머스 징검다리 (JAVA) (0) | 2023.08.14 |
프로그래머스 순위 (JAVA) (0) | 2023.08.13 |
프로그래머스 에어컨 (JAVA) (0) | 2023.08.12 |