-
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;
}
}