-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path475.Heaters.py
36 lines (31 loc) · 1009 Bytes
/
475.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
26
27
28
29
30
31
32
33
34
35
36
# https://leetcode.com/problems/heaters/
#
# algorithms
# Easy (30.79%)
# Total Accepted: 41,152
# Total Submissions: 133,635
# beats 99.53% of python submissions
class Solution(object):
def findRadius(self, houses, heaters):
"""
:type houses: List[int]
:type heaters: List[int]
:rtype: int
"""
houses.sort()
heaters.sort()
res = 0
heater_idx = 0
heaters_len = len(heaters)
for h in houses:
while heater_idx < heaters_len and heaters[heater_idx] < h:
heater_idx += 1
if heater_idx == 0:
res = max(res, heaters[heater_idx] - h)
elif heater_idx == heaters_len:
res = max(res, houses[-1] - heaters[-1])
break
else:
if heaters[heater_idx - 1] + res < h < heaters[heater_idx] - res:
res = min(h - heaters[heater_idx - 1], heaters[heater_idx] - h)
return res