475. 供暖器
为保证权益,题目请参考 475. 供暖器(From LeetCode).
解决方案1
Python
python
# 475. 供暖器
# https://leetcode-cn.com/problems/heaters/
from typing import List
class Solution:
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
houses.sort()
heaters.sort()
distance_min = 0
heater_index = 0
for house_index in range(len(houses)):
while heater_index + 1 < len(heaters) and abs(heaters[heater_index] - houses[house_index]) >= abs(heaters[heater_index+1] - houses[house_index]):
heater_index += 1
distance_min = max(distance_min, abs(heaters[heater_index] - houses[house_index]))
return distance_min
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21