forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYJ_43236.java
More file actions
60 lines (52 loc) · 2.08 KB
/
YJ_43236.java
File metadata and controls
60 lines (52 loc) · 2.08 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Arrays;
/***
* 알고리즘: 이분탐색
* 시간복잡도: 1 ≤ distance ≤ 10^9 크기로 O(logN)
* 아이디어:
* 이분탐색의 중간 타겟을 구간의 0~distance 거리의 중간점으로 해서 구간의 최소거리를 구하는 이유?
* 중간 값은 이분탐색을 위한 최소 거리의 후보이고, 이 값을 기준으로 바위 사이의 구간이 가능한 최소 거리인지 확인
* 제거해야 하는 바위 수를 세는 이유?
* 돌을 제거하지 않으면, 어떤 돌 사이의 거리가 너무 짧아 최소 거리를 크게 만들 수 없음
* 만약 돌을 제거하면 제거한 길이만큼 다음 바위의 구간이 늘어남
*/
public class YJ_43236 {
public static void main(String[] args) {
int distance = 25;
int[] rocks = {2, 14, 11, 21, 17};
int n = 2;
System.out.println(solution(distance,rocks,n));
}
static int solution(int distance, int[] rocks, int n) {
int answer = 0;
Arrays.sort(rocks);
//중간값을 어떤걸로 정할것인가
//어떤 기준에서 left 또는 right를 움직일 것인가?
int left= 0;
int right=distance;
while(left<=right){
int mid = (left+right)/2;
int remove =0;
int current=0;
for(int i=0; i<rocks.length; i++){
//다음 바위-현재바위가 현재 최소구간보다 작을 경우 최소값 중 큰값 후보에서 제외
//제외 == 제거해야 하는 바위
if(mid > rocks[i]-current){
remove++;
continue;
}
current = rocks[i];
}
//도착구간-마지막 바위
if(mid > distance - current){
remove++;
}
if(remove <= n){
answer= Math.max(answer,mid);
left = mid+1; //더 큰값을 탐색해서 최소거리 최대화
}else{
right = mid-1;
}
}
return answer;
}
}