forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
heaters.py
26 lines (22 loc) · 799 Bytes
/
heaters.py
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
# Time: O((m + n) * logn), m is the number of the houses, n is the number of the heaters.
# Space: O(1)
import bisect
class Solution(object):
def findRadius(self, houses, heaters):
"""
:type houses: List[int]
:type heaters: List[int]
:rtype: int
"""
heaters.sort()
min_radius = 0
for house in houses:
equal_or_larger = bisect.bisect_left(heaters, house)
curr_radius = float("inf")
if equal_or_larger != len(heaters):
curr_radius = heaters[equal_or_larger] - house
if equal_or_larger != 0:
smaller = equal_or_larger-1
curr_radius = min(curr_radius, house - heaters[smaller])
min_radius = max(min_radius, curr_radius)
return min_radius