MCPcopy
hub / github.com/qiyuangong/leetcode / findRadius

Method findRadius

java/475_Heaters.java:2–16  ·  view source on GitHub ↗
(int[] houses, int[] heaters)

Source from the content-addressed store, hash-verified

1public class Solution {
2 public int findRadius(int[] houses, int[] heaters) {
3 Arrays.sort(heaters);
4 int result = Integer.MIN_VALUE;
5
6 for (int house : houses) {
7 // Java binarySearch return - insertPoint - 1 if not found
8 // This point is greater than value you want
9 int index = Arrays.binarySearch(heaters, house);
10 if (index < 0) index = -(index + 1);
11 int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
12 int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;
13 result = Math.max(result, Math.min(dist1, dist2));
14 }
15 return result;
16 }
17}

Callers

nothing calls this directly

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected