Minimize Max Distance to Gas Station
You are given a sorted array stations of positions of existing gas stations along a horizontal line and an integer k. You may add exactly k new gas stations anywhere (positions need not be integers). Let penalty be the maximum distance between any two adjacent gas stations after the additions. Return the smallest possible penalty. Answers within 1e-6 of the true value are accepted.
Open official problem prompt ↗Find the smallest possible maximum spacing between adjacent gas stations after inserting k new ones.
You are placing rest stops on a highway. Given a budget of k new stops, you want the longest stretch without a stop to be as short as possible. You guess a maximum acceptable stretch, check how many stops that guess demands, and tighten the guess.
- Input
- stations = [1,2,3,4,5,6,7,8,9,10], k = 9
- Output
- 0.50000
- Why
- There are nine unit gaps; placing one new station in each gap halves every gap to 0.5, and no smaller maximum is achievable with only nine stations.
10 <= stations.length <= 20000 <= stations[i] <= 10^8stations is sorted in strictly increasing order1 <= k <= 10^6Answers within 10^-6 of the correct value are accepted