|
44 | 44 | <li><code>1 <= houses[i], heaters[i] <= 10<sup>9</sup></code></li>
|
45 | 45 | </ul>
|
46 | 46 |
|
47 |
| - |
48 | 47 | ## Solutions
|
49 | 48 |
|
50 | 49 | <!-- tabs:start -->
|
51 | 50 |
|
52 | 51 | ### **Python3**
|
53 | 52 |
|
54 | 53 | ```python
|
55 |
| - |
| 54 | +class Solution: |
| 55 | + def findRadius(self, houses: List[int], heaters: List[int]) -> int: |
| 56 | + houses.sort() |
| 57 | + heaters.sort() |
| 58 | + |
| 59 | + def check(r): |
| 60 | + m, n = len(houses), len(heaters) |
| 61 | + i = j = 0 |
| 62 | + while i < m: |
| 63 | + if j >= n: |
| 64 | + return False |
| 65 | + mi = heaters[j] - r |
| 66 | + mx = heaters[j] + r |
| 67 | + if houses[i] < mi: |
| 68 | + return False |
| 69 | + if houses[i] > mx: |
| 70 | + j += 1 |
| 71 | + else: |
| 72 | + i += 1 |
| 73 | + return True |
| 74 | + |
| 75 | + left, right = 0, int(1e9) |
| 76 | + while left < right: |
| 77 | + mid = (left + right) >> 1 |
| 78 | + if check(mid): |
| 79 | + right = mid |
| 80 | + else: |
| 81 | + left = mid + 1 |
| 82 | + return left |
56 | 83 | ```
|
57 | 84 |
|
58 | 85 | ### **Java**
|
59 | 86 |
|
60 | 87 | ```java
|
| 88 | +class Solution { |
| 89 | + public int findRadius(int[] houses, int[] heaters) { |
| 90 | + Arrays.sort(heaters); |
| 91 | + int res = 0; |
| 92 | + for (int x : houses) { |
| 93 | + int i = Arrays.binarySearch(heaters, x); |
| 94 | + if (i < 0) { |
| 95 | + i = ~i; |
| 96 | + } |
| 97 | + int dis1 = i > 0 ? x - heaters[i - 1] : Integer.MAX_VALUE; |
| 98 | + int dis2 = i < heaters.length ? heaters[i] - x : Integer.MAX_VALUE; |
| 99 | + res = Math.max(res, Math.min(dis1, dis2)); |
| 100 | + } |
| 101 | + return res; |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### **C++** |
| 107 | + |
| 108 | +```cpp |
| 109 | +class Solution { |
| 110 | +public: |
| 111 | + int findRadius(vector<int>& houses, vector<int>& heaters) { |
| 112 | + sort(houses.begin(), houses.end()); |
| 113 | + sort(heaters.begin(), heaters.end()); |
| 114 | + int left = 0, right = 1e9; |
| 115 | + while (left < right) { |
| 116 | + int mid = left + right >> 1; |
| 117 | + if (check(houses, heaters, mid)) right = mid; |
| 118 | + else left = mid + 1; |
| 119 | + } |
| 120 | + return left; |
| 121 | + } |
| 122 | + |
| 123 | + bool check(vector<int>& houses, vector<int>& heaters, int r) { |
| 124 | + int m = houses.size(), n = heaters.size(); |
| 125 | + int i = 0, j = 0; |
| 126 | + while (i < m) |
| 127 | + { |
| 128 | + if (j >= n) return false; |
| 129 | + int mi = heaters[j] - r; |
| 130 | + int mx = heaters[j] + r; |
| 131 | + if (houses[i] < mi) return false; |
| 132 | + if (houses[i] > mx) ++j; |
| 133 | + else ++i; |
| 134 | + } |
| 135 | + return true; |
| 136 | + } |
| 137 | +}; |
| 138 | +``` |
61 | 139 |
|
| 140 | +### **Go** |
| 141 | + |
| 142 | +```go |
| 143 | +func findRadius(houses []int, heaters []int) int { |
| 144 | + sort.Ints(houses) |
| 145 | + sort.Ints(heaters) |
| 146 | + m, n := len(houses), len(heaters) |
| 147 | + |
| 148 | + check := func(r int) bool { |
| 149 | + var i, j int |
| 150 | + for i < m { |
| 151 | + if j >= n { |
| 152 | + return false |
| 153 | + } |
| 154 | + mi, mx := heaters[j]-r, heaters[j]+r |
| 155 | + if houses[i] < mi { |
| 156 | + return false |
| 157 | + } |
| 158 | + if houses[i] > mx { |
| 159 | + j++ |
| 160 | + } else { |
| 161 | + i++ |
| 162 | + } |
| 163 | + } |
| 164 | + return true |
| 165 | + } |
| 166 | + left, right := 0, int(1e9) |
| 167 | + for left < right { |
| 168 | + mid := (left + right) >> 1 |
| 169 | + if check(mid) { |
| 170 | + right = mid |
| 171 | + } else { |
| 172 | + left = mid + 1 |
| 173 | + } |
| 174 | + } |
| 175 | + return left |
| 176 | +} |
62 | 177 | ```
|
63 | 178 |
|
64 | 179 | ### **...**
|
|
0 commit comments