-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode475.java
64 lines (58 loc) · 2.29 KB
/
LeetCode475.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Arrays;
public class LeetCode475 {
public static void main(String[] args) {
// 输入: houses = [1,2,3], heaters = [2]
// 输出: 1
System.out.println(new Solution475().findRadius(new int[] { 1, 2, 3 }, new int[] { 2 }));
// 输入: houses = [1,2,3,4], heaters = [1,4]
// 输出: 1
System.out.println(new Solution475().findRadius(new int[] { 1, 2, 3, 4 }, new int[] { 1, 4 }));
// 输入:houses = [1,5], heaters = [2]
// 输出:3
System.out.println(new Solution475().findRadius(new int[] { 1, 5 }, new int[] { 2 }));
// 输入:houses =
// [282475249,622650073,984943658,144108930,470211272,101027544,457850878,458777923],
// heaters =
// [823564440,115438165,784484492,74243042,114807987,137522503,441282327,16531729,823378840,143542612]
// 输出:161834419
System.out.println(new Solution475().findRadius(
new int[] { 282475249, 622650073, 984943658, 144108930, 470211272, 101027544, 457850878, 458777923 },
new int[] { 823564440, 115438165, 784484492, 74243042, 114807987, 137522503, 441282327, 16531729,
823378840, 143542612 }));
}
}
class Solution475 {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters);
int left = 0;
int right = Math.max(houses[houses.length - 1], heaters[heaters.length - 1]);
int mid;
while (left < right) {
mid = left + (right - left) / 2;
if (isValid(mid, houses, heaters)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
public boolean isValid(int radius, int[] houses, int[] heaters) {
int p1 = 0;
int p2 = 0;
while (p1 < houses.length) {
if (Math.abs(houses[p1] - heaters[p2]) <= radius) {
p1++;
} else {
p2++;
if (p2 == heaters.length) {
// System.out.println(String.format("%d is Invalid", radius));
return false;
}
}
}
// System.out.println(String.format("%d is valid", radius));
return true;
}
}