-
Notifications
You must be signed in to change notification settings - Fork 16
/
heaters.py
24 lines (21 loc) · 970 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
from typing import List
class Solution:
def binary_search(self, array: List[int], element: int) -> int:
left, right = 0, len(array) - 1
while left <= right:
middle = (left + right) // 2
if array[middle] == element: return middle
elif array[middle] < element: left = middle + 1
else: right = middle - 1
return left
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
heaters.sort()
minRadius, infinity = 0, float('inf')
for house in houses:
index = self.binary_search(heaters, house)
if index < len(heaters) and house == heaters[index]:
continue
leftRadius = infinity if index == 0 else house - heaters[index - 1]
rightRadius = infinity if index == len(heaters) else heaters[index] - house
minRadius = max(minRadius, min(leftRadius, rightRadius))
return minRadius