Skip to content

Commit fb24854

Browse files
committed
heaters
1 parent 7615e2d commit fb24854

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Heaters.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Winter is coming! Your first job during the contest is to design a standard heater
2+
# with fixed warm radius to warm all the houses.
3+
#
4+
# Now, you are given positions of houses and heaters on a horizontal line, find out
5+
# minimum radius of heaters so that all houses could be covered by those heaters.
6+
#
7+
# So, your input will be the positions of houses and heaters seperately, and your
8+
# expected output will be the minimum radius standard of heaters.
9+
#
10+
# Note:
11+
#
12+
# Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
13+
# Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
14+
# As long as a house is in the heaters' warm radius range, it can be warmed.
15+
# All the heaters follow your radius standard and the warm radius will the same.
16+
#
17+
# Input: [1,2,3],[2]
18+
# Output: 1
19+
# Explanation: The only heater was placed in the position 2, and if we use the
20+
# radius 1 standard, then all the houses can be warmed.
21+
#
22+
#
23+
# Input: [1,2,3,4],[1,4]
24+
# Output: 1
25+
# Explanation: The two heater was placed in the position 1 and 4. We need to use
26+
# radius 1 standard, then all the houses can be warmed.
27+
28+
29+
class Solution:
30+
def findRadius(self, houses, heaters):
31+
houses.sort();
32+
heaters.sort()
33+
heaters = [float("-inf")] + heaters + [float("inf")]
34+
35+
i = j = radius = 0
36+
37+
while i < len(houses):
38+
if heaters[j] >= houses[i]:
39+
dist = min(heaters[j] - houses[i], houses[i] - heaters[j - 1])
40+
i += 1
41+
radius = max(radius, dist)
42+
43+
else:
44+
j += 1
45+
46+
return radius

0 commit comments

Comments
 (0)