15% off all plans & roadmaps Use code
See plansLeetCode 774 · Solution & editorial
You are given a sorted array stations of length n, where stations[i] represents the position of the ith gas station along a number line. You are also given an integer k, representing the number of additional gas stations you may add at any position along the number line.
Minimize Max Distance to Gas Station is a Binary Search Patterns problem, in the On Answer / Condition Function subpattern. The pattern covers standard binary search, rotated arrays, first/last occurrence, and search on answer space
Learn binary search on answer space for optimization problems like Koko eating bananas and capacity to ship packages.
The reference solution runs in O(N * log(R/eps)) time and O(1) space.
Minimize Max Distance to Gas Station has been reported in interviews at Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Minimize Max Distance to Gas Station is a Binary Search Patterns problem, in the On Answer / Condition Function subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.