Skip to content

[3주차] 배수빈 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 28, 2024
Prev Previous commit
Next Next commit
배수빈: [PG] 43236 징검다리_240926
  • Loading branch information
baexxbin committed Sep 26, 2024
commit e504d55f13fc5b595bc5bb6ef1706504b284b0b9
35 changes: 35 additions & 0 deletions Programmers/Level4/SB_43236.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Programmers.Level4;

import java.util.Arrays;

public class SB_43236 {
private int removeCnt(int x, int[] rocks, int arrive) { // x가 돌 사이간 최소거리가되려면 몇개의 돌을 지워야하는지
int pre = 0;
int remove = 0;
for(int i=0; i<rocks.length; i++){
if(rocks[i]-pre < x){
remove++;
continue;
}
pre = rocks[i];
}
if (arrive-pre < x) remove++;

return remove;
}
public int solution(int distance, int[] rocks, int n) {
Arrays.sort(rocks);
int left = 1;
int right = distance;

int ans = 0;
while(left <=right){
int mid = (left+right)/2;
if(removeCnt(mid, rocks, distance) <= n){ // mid가 가능하면 길이 키워보기
ans = Math.max(ans, mid);
left = mid+1;
} else right = mid-1;
}
return ans;
}
}