|
2 | 2 |
|
3 | 3 | [https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/](https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/) |
4 | 4 |
|
5 | | -On a horizontal number line, we have gas stations at positions stations\[0\], stations\[1\], ..., stations\[N-1\], where N = stations.length. |
| 5 | +On a horizontal number line, we have gas stations at positions stations\[0\], stations\[1\], ..., stations\[N-1\], where N = stations.length. |
6 | 6 |
|
7 | | -Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized. |
| 7 | +Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized. |
8 | 8 |
|
9 | 9 | Return the smallest possible value of D. |
10 | 10 |
|
@@ -32,7 +32,14 @@ Output: 0.500000 |
32 | 32 | 4. Then the answer will be on top of the heap |
33 | 33 | 5. Time complexity O\(Klogn\) |
34 | 34 | 6. Space complexity O\(n\) |
35 | | -3. ASD |
| 35 | +3. Binary Search |
| 36 | + 1. Using the boundaries of stations, the possible width will be in between 0 and 1e8 |
| 37 | + 2. Perform binary search on the width within these boundaries, we will narrow down our search until these boundaries differ less than our desired delta |
| 38 | + 3. Now the conditions to narrow our search to left or right depends on a separate function possible\(mi, stations, K\), where it check if we can use less than or equal to K stations to achieve the mi distance |
| 39 | + 4. If possible return true, we set hi = mi to find better distance |
| 40 | + 5. Else we set lo = mi |
| 41 | + 6. Time complexity O\(nlogW\), where w = 1e8/1e-6 = 1e14 |
| 42 | + 7. Space complexity O\(1\) |
36 | 43 |
|
37 | 44 | ### Solution |
38 | 45 |
|
|
0 commit comments