-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (31 loc) · 1.03 KB
/
Solution.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
34
35
36
class Solution {
public int findRadius(int[] houses, int[] heaters) {
int res = 0;
Arrays.sort(heaters);
for (int house : houses) {
int low = 0,
high = heaters.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (heaters[mid] > house) {
high = mid - 1;
} else if (heaters[mid] < house) {
low = mid + 1;
} else {
low = mid;
break;
}
}
int distance = 0;
if (low == 0) {
distance = heaters[low] - house;
} else if (low == heaters.length) {
distance = house - heaters[low - 1];
} else {
distance = Math.min(heaters[low] - house, house - heaters[low - 1]);
}
res = Math.max(res, distance);
}
return res;
}
}