Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Heaters problem solution

YASH PAL, 31 July 202422 January 2026

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

Leetcode Heaters 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

Heaters 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 solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes