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