| 1 | public 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 | } |