How to Find Heaters in Python ?

  Python Questions & Answers

Dear reader, I will present today in this article how to find heaters in Python?

If you have to design a standard heater with a fixed heating radius to heat all the houses. Now, we have given the position of the houses and heaters on a horizontal line, we have to find the minimum radius of the heaters so that all the houses are covered by those heaters. Therefore, we will provide homes and heaters separately, and our expected output will be the minimum radius standard of the heaters.

So, if the input is [1,2,3,4], like [1,4], the output will be 1 because the two heaters were placed in position 1 and 4. We have to use radius 1, then all. The houses can be heated.

Example for Heaters in Python

from bisect import bisect_left
class SolvePorg:
   def findRadius(self, houses, heaters):
      houses.sort()
      heaters.sort()
      res = [float('inf')]*len(houses)
      for i in range(len(houses)):
         h = houses[i]
         ind = bisect_left(heaters, h)
         if ind==len(heaters):
            res[i] = min(res[i], abs(h - heaters[-1]))
         elif ind == 0:
            res[i] = min(res[i], abs(h - heaters[0]))
         else:
            res[i] = min(res[i], abs(h - heaters[ind]), abs(h - heaters[ind-1]))
      return max(res)

ob = SolvePorg()
print(ob.findRadius([1,2,3,4],[1,4]))

Output

1

LEAVE A COMMENT