774. Minimize Max Distance to Gas Station
Minimize Max Distance to Gas Station (LeetCode 774) is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Binary Search Patterns pattern, in the On Answer / Condition Function subpattern.
How to solve Minimize Max Distance to Gas Station
Minimize Max Distance to Gas Station is a Binary Search on Answer Space problem, part of Binary Search Patterns. Learn binary search on answer space for optimization problems like Koko eating bananas and capacity to ship packages.
Work towards the solution in these steps:
- Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?"
The reference solution runs in O(N * log(R/eps)) time and O(1) space.
The Minimize Max Distance to Gas Station solution and editorial walks through the full approach with reference code.
Problem statement
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.
Your task is to add exactly k new gas stations such that the maximum distance between any two adjacent gas stations is minimized.
Return the minimum possible value of the maximum distance between adjacent gas stations after adding the k new stations. The answer should be returned as a floating point number with an absolute error less than 1e-6.
Examples
Example 1
Input: {"stations": [0, 10], "k": 1}
Output: 5.0
Explanation: One station to add between 0 and 10, so the largest distance is minimized to 5.0 by placing at 5.
Example 2
Input: {"stations": [0, 10], "k": 2}
Output: 3.3333333333333335
Explanation: Two stations to add, so the interval is split into three equal parts of length 10/3.
Topics and companies
Topics: Array, Binary Search.
Reported in interviews at Google.
How to practise Minimize Max Distance to Gas Station on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N * log(R/eps)) time and O(1) space.
Open Minimize Max Distance to Gas Station in the code editor, or read the Minimize Max Distance to Gas Station editorial for a worked solution with its approach and complexity analysis.
Where Minimize Max Distance to Gas Station sits in the DSA pattern sheet
- Binary Search Patterns pattern guide
- On Answer / Condition Function subpattern
- The full DSA patterns sheet
Problems related to Minimize Max Distance to Gas Station
Other problems that use the same On Answer / Condition Function and Binary Search Patterns techniques:
- Capacity To Ship Packages Within D Days — Medium, same subpattern
- Koko Eating Bananas — Medium, same subpattern
- Maximum Candies Allocated to K Children — Medium, same subpattern
- Minimized Maximum of Products Distributed to Any Store — Medium, same subpattern
- Minimum Limit of Balls in a Bag — Medium, same subpattern
- Minimum Number of Days to Make m Bouquets — Medium, same subpattern
Minimize Max Distance to Gas Station FAQ
What is the LeetCode number of Minimize Max Distance to Gas Station?
Minimize Max Distance to Gas Station is LeetCode problem 774, rated Hard.
What pattern is used to solve Minimize Max Distance to Gas Station?
Minimize Max Distance to Gas Station is a Binary Search on Answer Space problem. It belongs to the Binary Search Patterns pattern, in the On Answer / Condition Function subpattern.
What is the time complexity of Minimize Max Distance to Gas Station?
The reference solution runs in O(N * log(R/eps)) time and O(1) space.
Which companies ask Minimize Max Distance to Gas Station?
Minimize Max Distance to Gas Station has been reported in interviews at Google.