Skip to content

Commit 54693e8

Browse files
authored
[20251025] PGM / LV4 / 징검다리 / 강신지
1 parent 08fe7bc commit 54693e8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int solution(int distance, int[] rocks, int n) {
6+
Arrays.sort(rocks);
7+
8+
int start = 0;
9+
int end = distance;
10+
int answer = 0;
11+
12+
while (start <= end) {
13+
int mid = (start+end)/2;
14+
int cnt = cntRemove(rocks, mid, distance);
15+
if (cnt <= n){
16+
answer = Math.max(mid, answer);
17+
start = mid + 1;
18+
} else {
19+
end = mid - 1;
20+
}
21+
}
22+
23+
return answer;
24+
}
25+
26+
private int cntRemove(int[] rocks, int mid, int distance){
27+
int start = 0;
28+
int cnt = 0;
29+
30+
for (int i=0; i<rocks.length; i++){
31+
if (rocks[i] - start >= mid) {
32+
start = rocks[i];
33+
} else {
34+
cnt++;
35+
}
36+
}
37+
38+
if (distance - start < mid){
39+
cnt++;
40+
}
41+
return cnt;
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)