Minimize Maximum Distance Between Gas Stations
You are given a sorted list of integers representing the positions of existing gas stations along a straight highway. You are also given an integer k, representing how many additional gas stations you are allowed to build. Your task is to place these new stations anywhere along the highway (not necessarily at integer positions) such that the maximum distance between any two adjacent stations is as small as possible. You must then return this minimized distance as a floating-point number.
To understand the problem intuitively, imagine the highway as a number line and each station as a point. Initially, the largest distance between two adjacent stations represents the worst-case distance a driver might need to travel without refueling. Each new station you add can be placed strategically to reduce that worst distance. The goal is to find the smallest possible maximum interval between any two consecutive stations after adding exactly k new ones. Because stations can be added at fractional positions, your answer can be a decimal value, and it should be computed to an acceptable precision (e.g., within 1e-6).
To solve this problem conceptually, you must reason about how to search for the optimal threshold distance. For example, if a chosen distance d is too small, you would need more than k stations to achieve it. If it is too large, it means it’s possible to use fewer or exactly k stations. Your goal is to find the smallest d that satisfies the constraint using no more than k new stations. Return the resulting minimal possible maximum distance as a real number that represents the tightest achievable configuration of stations.
Example 1:
Input: stations = [1, 2, 3, 4, 5, 6, 7, 8, 9], k = 3
Output: 1.0
Explanation: Adding 3 stations evenly between existing ones can make all distances at most 1.
Example 2:
Input: stations = [1, 10], k = 1
Output: 4.5
Explanation: Adding one station at position 5.5 minimizes the maximum gap to 4.5.
Example 3:
Input: stations = [1, 2, 10], k = 2
Output: 3.0
Explanation: Adding stations at positions 4 and 7 results in gaps of at most 3.
Related Problems
No related problems found
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
