-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaters.java
More file actions
43 lines (33 loc) · 1.13 KB
/
Copy pathHeaters.java
File metadata and controls
43 lines (33 loc) · 1.13 KB
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
37
38
39
40
41
42
43
package leetcode.easy.page3;
import java.util.Arrays;
public class Heaters {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int radius = 0;
for (int i = 0; i < houses.length; i++) {
int dis = this.binarySearchMinDistance(heaters, 0, heaters.length - 1, houses[i]);
radius = Math.max(radius, dis);
}
return radius;
}
/**
* Find the min distance from the house to the heaters
* @param heaters
* @param l
* @param r
* @param housePos
* @return
*/
private int binarySearchMinDistance(int[] heaters, int l, int r, int housePos) {
if (r - l <= 1) {
return Math.min(Math.abs(housePos - heaters[l]), Math.abs(heaters[r] - housePos));
}
int mid = (l + r) / 2;
if (heaters[mid] == housePos || heaters[l] == housePos || heaters[r] == housePos) return 0;
if (housePos < heaters[mid]) {
return binarySearchMinDistance(heaters, l, mid, housePos);
} else {
return binarySearchMinDistance(heaters, mid, r, housePos);
}
}
}