forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSB_43236.java
More file actions
33 lines (30 loc) · 983 Bytes
/
SB_43236.java
File metadata and controls
33 lines (30 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Arrays;
public class SB_43236 {
private int removeCnt(int x, int[] rocks, int arrive) { // x가 돌 사이간 최소거리가되려면 몇개의 돌을 지워야하는지
int pre = 0;
int remove = 0;
for(int i=0; i<rocks.length; i++){
if(rocks[i]-pre < x){
remove++;
continue;
}
pre = rocks[i];
}
if (arrive-pre < x) remove++;
return remove;
}
public int solution(int distance, int[] rocks, int n) {
Arrays.sort(rocks);
int left = 1;
int right = distance;
int ans = 0;
while(left <=right){
int mid = (left+right)/2;
if(removeCnt(mid, rocks, distance) <= n){ // mid가 가능하면 길이 키워보기
ans = Math.max(ans, mid);
left = mid+1;
} else right = mid-1;
}
return ans;
}
}