-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (29 loc) · 877 Bytes
/
Solution.java
File metadata and controls
34 lines (29 loc) · 877 Bytes
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
import java.util.Arrays;
class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters);
int l = 0, r = Math.max(houses[houses.length-1], heaters[heaters.length-1]);
while(l<r){
int mid = l+(r-l)/2;
if(check(mid,houses,heaters)){
r = mid;
}else{
l = mid + 1;
}
}
return r;
}
private boolean check(int mid, int[] houses, int[] heaters){
//for each house, find first heaters that covers it
for(int i = 0, j = 0; i<houses.length;i++){
while(j<heaters.length && Math.abs(houses[i]-heaters[j])>mid){
j++;
}
if(j>=heaters.length){
return false;
}
}
return true;
}
}