-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindRadius.java
More file actions
33 lines (26 loc) · 783 Bytes
/
Copy pathfindRadius.java
File metadata and controls
33 lines (26 loc) · 783 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
package leetcode;
import java.util.Arrays;
//leetcode475
public class findRadius {
public static int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters);
int n = houses.length;
int m = heaters.length;
int radius = 0;
int index = 0;
for (int i=0;i<n;i++) {
while(houses[i]> heaters[index] && index<m-1) {
index++;
}
int cur;
if(index>0) {
cur = Math.min(Math.abs(heaters[index]-houses[i]),Math.abs(heaters[index-1]-houses[i]));
} else {
cur = Math.abs(heaters[index]-houses[i]);
}
radius = Math.max(radius,cur);
}
return radius;
}
}