Leetcode 475: Heaters

grid47
grid47
Exploring patterns and algorithms
Sep 20, 2024 6 min read

A grid where heaters light up and warm nearby houses, with each heater softly illuminating its effect.
Solution to LeetCode 475: Heaters Problem

You are given the positions of houses and heaters along a horizontal line. Your task is to find the minimum radius required for the heaters so that all houses are within the heater’s warm radius.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays: `houses` representing the positions of the houses and `heaters` representing the positions of the heaters.
Example: houses = [2, 5, 7, 10], heaters = [1, 8]
Constraints:
• 1 <= houses.length, heaters.length <= 3 * 10^4
• 1 <= houses[i], heaters[i] <= 10^9
Output: Return the minimum radius required for the heaters to cover all houses.
Example: Output: 3
Constraints:
• The output should be an integer representing the minimum radius for heaters.
Goal: Find the minimum radius required for heaters to ensure all houses are covered.
Steps:
• 1. Sort the positions of both the houses and the heaters.
• 2. For each house, calculate the nearest heater and determine the distance between them.
• 3. The largest distance across all houses will determine the required radius of the heaters.
Goal: The constraints are based on the number of houses, heaters, and their positions.
Steps:
• The length of the `houses` and `heaters` arrays is between 1 and 30,000.
• The positions of the houses and heaters range between 1 and 10^9.
Assumptions:
• The heaters can be placed at any position along the horizontal line.
• There is no overlap in the positions of houses and heaters.
Input: houses = [2, 5, 7, 10], heaters = [1, 8]
Explanation: Heaters at positions 1 and 8 with a radius of 3 can cover all the houses.

Input: houses = [5, 10], heaters = [6]
Explanation: A heater at position 6 with a radius of 4 can cover both houses at positions 5 and 10.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus