-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_43236.java
More file actions
40 lines (34 loc) · 1.15 KB
/
JY_43236.java
File metadata and controls
40 lines (34 loc) · 1.15 KB
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
34
35
36
37
38
39
import java.util.*;
class JY_43236 {
public int solution(int distance, int[] rocks, int n) {
int answer = 0;
List<Integer> rList = new ArrayList<>();
for(int i=0; i<rocks.length; i++) {
rList.add(rocks[i]);
}
rList.add(distance); // 도착지점 추가
Collections.sort(rList);
int s = 0;
int e = distance;
while(s <= e) {
int mid = (s + e) / 2;
int cnt = 0; // 제거해야할 바위 수
int pre = 0; // 바로 직전의 바위
for(int r: rList) {
int dist = r - pre;
// 두 바위 사이거리가 최소 거리보다 작음 => 안됨, 제거
if(dist < mid) cnt++;
else pre = r;
if(cnt > n) break;
}
// 없애야할 바위가 n보다 작거나 같으면 정답 저장
if(cnt <= n) {
answer = mid;
s = mid + 1;
} else {
e = mid - 1;
}
}
return answer;
}
}