-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_475.py
More file actions
35 lines (27 loc) · 991 Bytes
/
task_475.py
File metadata and controls
35 lines (27 loc) · 991 Bytes
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
35
from typing import List
from bisect import bisect_left
class Solution:
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
houses.sort()
heaters.sort()
m = len(heaters)
def check(x):
segments = []
for i in range(m):
segments.append([heaters[i]-x, heaters[i]+x])
i = 0
for v in houses:
while i < m and not (segments[i][0] <= v <= segments[i][1]):
i += 1
return i < m
left, right = -1, int(1e9+228)
while right-left > 1:
mid = (left+right) // 2
if check(mid):
right = mid
else:
left = mid
return right
print(Solution().findRadius(houses = [1,2,3], heaters = [2]))
print(Solution().findRadius(houses = [1,2,3,4], heaters = [1,4]))
print(Solution().findRadius(houses = [1,5], heaters = [2]))