Minimize Max Distance to Gas Station - Problem
The Gas Station Optimization Challenge

Imagine you're a city planner tasked with optimizing gas station placement along a highway! You're given an integer array stations representing the positions of existing gas stations on the x-axis, and you have a budget to add exactly k new gas stations.

Your goal is to minimize the maximum distance between any two adjacent gas stations after adding the new ones. You can place new stations anywhere on the x-axis (not necessarily at integer positions).

Think of it this way: If the largest gap between stations is too big, drivers might run out of fuel! Your job is to strategically place new stations to make the worst-case scenario as good as possible.

Input: An array of station positions and number k of stations to add
Output: The smallest possible maximum distance between adjacent stations
Precision: Answers within 10-6 of the actual answer will be accepted

Input & Output

example_1.py โ€” Basic Case
$ Input: stations = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 9
โ€บ Output: 0.500000
๐Ÿ’ก Note: With 9 additional stations, we can place one between each pair of existing stations, making all gaps equal to 0.5
example_2.py โ€” Uneven Gaps
$ Input: stations = [23, 24, 36, 39, 46, 56, 57, 65, 84, 98], k = 1
โ€บ Output: 14.000000
๐Ÿ’ก Note: The largest gap is between positions 65 and 84 (length 19). Adding one station there creates two gaps of length 9.5, but other gaps remain larger. The maximum remaining gap is 14.
example_3.py โ€” Edge Case
$ Input: stations = [1, 13, 17, 23], k = 5
โ€บ Output: 4.000000
๐Ÿ’ก Note: Optimally distribute 5 stations among the gaps [12, 4, 6] to minimize the maximum gap to 4.0

Constraints

  • 2 โ‰ค stations.length โ‰ค 104
  • 0 โ‰ค stations[i] โ‰ค 108
  • stations is sorted in strictly increasing order
  • 1 โ‰ค k โ‰ค 106
  • Answers within 10-6 of the actual answer will be accepted

Visualization

Tap to expand
Gas Station Optimization VisualizationHighwayBefore: Uneven gapsGap: 100Gap: 300 (too large!)Gap: 200After: Optimized with k=2 stations10015010050200Binary Search Algorithm0300Testing: 150Question: Can we achieve max gap โ‰ค 150 with k=2 stations?Answer: Yes! (Need to split 300-gap into 3 parts = 100 each)Since feasible, search left half for smaller distances๐ŸŽฏ **Key Insight:** Binary search on the answer transforms optimization into decision problems!
Understanding the Visualization
1
Identify the Problem
We have existing gas stations with uneven gaps between them
2
Binary Search Strategy
Instead of finding positions, we search for the optimal maximum distance
3
Feasibility Test
For each candidate distance, check if achievable with k stations
4
Greedy Placement
Place stations greedily whenever gaps exceed our target distance
Key Takeaway
๐ŸŽฏ Key Insight: Transform the optimization problem into a series of decision problems using binary search on the answer, combined with greedy feasibility checking.
Asked in
Google 35 Amazon 28 Apple 22 Microsoft 18
20.8K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen