이분탐색
https://school.programmers.co.kr/learn/courses/30/lessons/43236
import java.util.*;
class Solution {
public int solution(int distance, int[] rocks, int n) {
int answer = distance;
int left = 0;
int right = distance;
Arrays.sort(rocks);
while(left <= right) {
int mid = left + (right - left) / 2;
int rockCnt = 0;
int front = 0;
boolean isPossible = false;
for(int back : rocks) {
int dist = back - front;
if(dist >= mid) {
front = back;
rockCnt++;
if(rockCnt == rocks.length - n) {
back = distance;
dist = back - front;
if(dist >= mid) {
left = mid + 1;
answer = mid;
isPossible = true;
}
break;
}
}
}
if(!isPossible) {
right = mid - 1;
}
}
return answer;
}
}
1시간