-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathHeaters.java
33 lines (32 loc) · 888 Bytes
/
Heaters.java
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
// Runtime: 15 ms (Top 66.0%) | Memory: 46.54 MB (Top 44.7%)
class Solution {
public boolean can(int r, int[] houses, int[] heaters) {
int prevHouseIdx = -1;
for(int i = 0; i < heaters.length; i++) {
int from = heaters[i]-r;
int to = heaters[i]+r;
for(int j = prevHouseIdx+1; j < houses.length; j++){
if(houses[j]<=to && houses[j]>=from){
prevHouseIdx++;
}
else break;
}
if(prevHouseIdx >= houses.length-1)return true;
}
return prevHouseIdx>= houses.length-1;
}
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters);
int lo = 0, hi = 1000000004;
int mid, ans = hi;
while(lo <= hi) {
mid = (lo+hi)/2;
if(can(mid, houses, heaters)){
ans = mid;
hi = mid - 1;
} else lo = mid + 1;
}
return ans;
}
}