Skip to content

Commit 2d11e40

Browse files
committed
Updates leetcode/binary-search/774-minimize-max-distance-to-gas-station.md
Auto commit by GitBook Editor
1 parent 015f1e6 commit 2d11e40

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
* [718-Maximum Length of Repeated Subarray](leetcode/binary-search/718-maximum-length-of-repeated-subarray.md)
262262
* [719-Find K-th Smallest Pair Distance](leetcode/binary-search/719-find-k-th-smallest-pair-distance.md)
263263
* [744-Find Smallest Letter Greater Than Target](leetcode/binary-search/744-find-smallest-letter-greater-than-target.md)
264+
* [774-Minimize Max Distance to Gas Station](leetcode/binary-search/774-minimize-max-distance-to-gas-station.md)
264265
* [Template](template.md)
265266
* [LeetCode](template/leetcode.md)
266267
* [Other](other.md)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
### Question {#question}
2+
3+
[https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/](https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/)
4+
5+
On a horizontal number line, we have gas stations at positions stations\[0\], stations\[1\], ..., stations\[N-1\], where N = stations.length.
6+
7+
Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized.
8+
9+
Return the smallest possible value of D.
10+
11+
**Example:**
12+
13+
```
14+
Input: stations = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 9
15+
Output: 0.500000
16+
```
17+
18+
### Thought Process {#thought-process}
19+
20+
1. Dynamic Programing \(TLE\)
21+
1. Create a dp array to store the best answer, where dp\[s\]\[k\] defines to be the answer for inserting k ending at jth station
22+
2. We initialize dp\[0\]\[j\] with the distance\[0\] / \(j + 1\), where j ranges from 0 to K
23+
3. We start loop for all distance segments from 1 to N - 1 using i, and nested loop for inserting extra stations from 0 to K using j
24+
4. For insert j stations for ith segment, we need to check all the previous k insertions ranges from 0 to j stations against the inserting the k stations in ith segment, the maximum of them is the candidate for our answer
25+
5. The answer dp\[i\]\[j\] is then the minimum of all the candiates
26+
6. Time complexity O\(nK^2\)
27+
7. Space complexity O\(nK\)
28+
2. Greedy Approach with Heap \(TLE\)
29+
1. Use heap to store each distance segments along with number of segments \(initially all 1\)
30+
2. Every time, we pull a max distance segment out considering along with number of segment and increase the number and pull it back
31+
3. After K operations, we have used up all the stations
32+
4. Then the answer will be on top of the heap
33+
5. Time complexity O\(Klogn\)
34+
6. Space complexity O\(n\)
35+
3. ASD
36+
37+
### Solution
38+
39+
```java
40+
class Solution {
41+
public double minmaxGasDist(int[] stations, int K) {
42+
int n = stations.length;
43+
double[][] dp = new double[n - 1][K + 1];
44+
double[] distances = new double[n - 1];
45+
for (int i = 1; i < n; i++) {
46+
distances[i - 1] = stations[i] - stations[i - 1];
47+
}
48+
for (int k = 0; k <= K; k++) {
49+
dp[0][k] = distances[0] / (k + 1);
50+
}
51+
for (int i = 1; i < n - 1; i++) {
52+
for (int j = 0; j <= K; j++) {
53+
double ans = 99999999;
54+
for (int k = 0; k <= j; k++) {
55+
ans = Math.min(ans, Math.max(dp[i - 1][k], distances[i] / (j - k + 1)));
56+
}
57+
dp[i][j] = ans;
58+
}
59+
}
60+
return dp[n - 2][K];
61+
}
62+
}
63+
```
64+
65+
```java
66+
class Solution {
67+
public double minmaxGasDist(int[] stations, int K) {
68+
PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> a[0] * 1.0 / a[1] > b[0] * 1.0 / b[1] ? -1 : 1);
69+
for (int i = 1; i < stations.length; i++) {
70+
maxHeap.offer(new int[]{stations[i] - stations[i - 1], 1});
71+
}
72+
int[] node = null;
73+
while (K-- > 0) {
74+
node = maxHeap.poll();
75+
node[1]++;
76+
maxHeap.offer(node);
77+
}
78+
node = maxHeap.poll();
79+
return node[0] * 1.0 / node[1];
80+
}
81+
}
82+
```
83+
84+
### Additional {#additional}
85+
86+
87+

0 commit comments

Comments
 (0)