File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments