Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Heaters problem solution

YASH PAL, 31 July 2024

In this Leetcode Heaters problem solution Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.

Every house can be warmed, as long as the house is within the heater’s warm radius range. 

Given the positions of houses and heaters on a horizontal line, return the minimum radius standard of heaters so that those heaters could cover all houses.

Notice that all the heaters follow your radius standard, and the warm radius will the same.

Leetcode Heaters problem solution

Problem solution in Python.

class Solution:
    def findRadius(self, houses: List[int], heaters: List[int]) -> int:
        
        nHeater = len(heaters)
        heaters.sort()
        maxDistance = - math.inf
        for house in houses:
            index = bisect_left(heaters, house)
            if index == 0:
                maxDistance = max(maxDistance, abs(house-heaters[index]))
            elif index == nHeater:
                maxDistance = max(maxDistance, abs(house-heaters[index-1]))
            else:
                maxDistance = max(maxDistance, min(abs(house-heaters[index-1]), abs(house-heaters[index])))
        return maxDistance

Problem solution in Java.

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        int[] array = new int[houses.length];
        int max = 0;
        Arrays.fill(array, Integer.MAX_VALUE);
        for(int i = 0; i < heaters.length; i++){
            int heat = heaters[i];
            for(int j = 0; j < houses.length; j++){
                if(houses[j] == heat){
                    array[j] = 0;
                }else{
                    array[j] = Math.min(array[j], Math.abs(houses[j] - heat));
                }
                
            }
        }
        Arrays.sort(array);
        return array[array.length - 1];
    }
}

Problem solution in C++.

class Solution {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        int n=houses.size();
        int m=heaters.size();
        vector<int> a(n, INT_MAX);
        
        for(int i=0,j=0;i<n && j<m;){
            if(houses[i]<=heaters[j]){
                a[i]=heaters[j]-houses[i];
                i++;
            }
            else
                j++;
        }
        
        for(int i=n-1,j=m-1;i>=0 && j>=0;){
            if(houses[i]>=heaters[j]){
                a[i]=min(houses[i]-heaters[j],a[i]);
                i--;
            }
            else
                j--;
        }
        
        int ans=0;
        for(int i=0;i<n;i++)
            ans=max(ans,a[i]);
        return ans;
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes