-
Notifications
You must be signed in to change notification settings - Fork 4
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;
}
}