Python 8 Lines Short and Clean

  • 1

    For each house find distance to closest heater. Take the max distance of these which is the radius needed.

    class Solution(object):
        def findRadius(self, houses, heaters):
            j,radius = 0,0
            for house in houses:
                while j+1 < len(heaters) and abs(heaters[j+1]-house) <= abs(heaters[j]-house):
                    j += 1
                radius = max(radius,abs(heaters[j]-house))
            return radius

  • 0

    really nice solution, easy to understand, thanks alot

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.