|
2 | 2 |
|
3 | 3 | import java.util.Arrays;
|
4 | 4 |
|
5 |
| -/** |
6 |
| - * 475. |
7 |
| - * |
8 |
| - Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. |
9 |
| -
|
10 |
| - Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. |
11 |
| -
|
12 |
| - So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. |
13 |
| -
|
14 |
| - Note: |
15 |
| - Numbers of houses and heaters you are given are non-negative and will not exceed 25000. |
16 |
| - Positions of houses and heaters you are given are non-negative and will not exceed 10^9. |
17 |
| - As long as a house is in the heaters' warm radius range, it can be warmed. |
18 |
| - All the heaters follow your radius standard and the warm radius will the same. |
19 |
| -
|
20 |
| - Example 1: |
21 |
| - Input: [1,2,3],[2] |
22 |
| - Output: 1 |
23 |
| - Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed. |
24 |
| -
|
25 |
| - Example 2: |
26 |
| - Input: [1,2,3,4],[1,4] |
27 |
| - Output: 1 |
28 |
| - Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed. |
29 |
| - */ |
30 | 5 | public class _475 {
|
31 | 6 |
|
32 |
| - public static class Solution1 { |
33 |
| - //credit: https://discuss.leetcode.com/topic/71460/short-and-clean-java-binary-search-solution |
34 |
| - public int findRadius(int[] houses, int[] heaters) { |
35 |
| - Arrays.sort(heaters); |
36 |
| - int radius = Integer.MIN_VALUE; |
37 |
| - for (int house : houses) { |
38 |
| - int index = Arrays.binarySearch(heaters, house); |
39 |
| - if (index < 0) { |
40 |
| - index = ~index; |
41 |
| - } |
42 |
| - int distance1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; |
43 |
| - int distance2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; |
44 |
| - |
45 |
| - radius = Math.max(radius, Math.min(distance1, distance2)); |
46 |
| - } |
47 |
| - return radius; |
48 |
| - } |
49 |
| - } |
| 7 | + public static class Solution1 { |
| 8 | + //credit: https://discuss.leetcode.com/topic/71460/short-and-clean-java-binary-search-solution |
| 9 | + public int findRadius(int[] houses, int[] heaters) { |
| 10 | + Arrays.sort(heaters); |
| 11 | + int radius = Integer.MIN_VALUE; |
| 12 | + for (int house : houses) { |
| 13 | + int index = Arrays.binarySearch(heaters, house); |
| 14 | + if (index < 0) { |
| 15 | + index = ~index; |
| 16 | + } |
| 17 | + int distance1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; |
| 18 | + int distance2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; |
| 19 | + |
| 20 | + radius = Math.max(radius, Math.min(distance1, distance2)); |
| 21 | + } |
| 22 | + return radius; |
| 23 | + } |
| 24 | + } |
50 | 25 | }
|
0 commit comments