题目描述
冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。
在加热器的加热半径范围内的每个房屋都可以获得供暖。
现在,给出位于一条水平线上的房屋 houses
和供暖器 heaters
的位置,请你找出并返回可以覆盖所有房屋的最小加热半径。
注意:所有供暖器 heaters
都遵循你的半径标准,加热的半径也一样。
示例 1:
输入: houses = [1,2,3], heaters = [2]
输出: 1
解释: 仅在位置 2 上有一个供暖器。如果我们将加热半径设为 1,那么所有房屋就都能得到供暖。
示例 2:
输入: houses = [1,2,3,4], heaters = [1,4]
输出: 1
解释: 在位置 1, 4 上有两个供暖器。我们需要将加热半径设为 1,这样所有房屋就都能得到供暖。
示例 3:
输入:houses = [1,5], heaters = [2]
输出:3
提示:
1 <= houses.length, heaters.length <= 3 * 104
1 <= houses[i], heaters[i] <= 109
解法
方法一
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 | class Solution:
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
houses.sort()
heaters.sort()
def check(r):
m, n = len(houses), len(heaters)
i = j = 0
while i < m:
if j >= n:
return False
mi = heaters[j] - r
mx = heaters[j] + r
if houses[i] < mi:
return False
if houses[i] > mx:
j += 1
else:
i += 1
return True
left, right = 0, int(1e9)
while left < right:
mid = (left + right) >> 1
if check(mid):
right = mid
else:
left = mid + 1
return left
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int res = 0;
for (int x : houses) {
int i = Arrays.binarySearch(heaters, x);
if (i < 0) {
i = ~i;
}
int dis1 = i > 0 ? x - heaters[i - 1] : Integer.MAX_VALUE;
int dis2 = i < heaters.length ? heaters[i] - x : Integer.MAX_VALUE;
res = Math.max(res, Math.min(dis1, dis2));
}
return res;
}
}
|
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 | class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int left = 0, right = 1e9;
while (left < right) {
int mid = left + right >> 1;
if (check(houses, heaters, mid))
right = mid;
else
left = mid + 1;
}
return left;
}
bool check(vector<int>& houses, vector<int>& heaters, int r) {
int m = houses.size(), n = heaters.size();
int i = 0, j = 0;
while (i < m) {
if (j >= n) return false;
int mi = heaters[j] - r;
int mx = heaters[j] + r;
if (houses[i] < mi) return false;
if (houses[i] > mx)
++j;
else
++i;
}
return true;
}
};
|
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
34 | func findRadius(houses []int, heaters []int) int {
sort.Ints(houses)
sort.Ints(heaters)
m, n := len(houses), len(heaters)
check := func(r int) bool {
var i, j int
for i < m {
if j >= n {
return false
}
mi, mx := heaters[j]-r, heaters[j]+r
if houses[i] < mi {
return false
}
if houses[i] > mx {
j++
} else {
i++
}
}
return true
}
left, right := 0, int(1e9)
for left < right {
mid := (left + right) >> 1
if check(mid) {
right = mid
} else {
left = mid + 1
}
}
return left
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | function findRadius(houses: number[], heaters: number[]): number {
houses.sort((a, b) => a - b);
heaters.sort((a, b) => a - b);
const m = houses.length,
n = heaters.length;
let ans = 0;
for (let i = 0, j = 0; i < m; i++) {
let cur = Math.abs(houses[i] - heaters[j]);
while (
j + 1 < n &&
Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1])
) {
cur = Math.min(Math.abs(houses[i] - heaters[++j]), cur);
}
ans = Math.max(cur, ans);
}
return ans;
}
|