Skip to content

Commit 50c051f

Browse files
committed
Updates leetcode/binary-search/774-minimize-max-distance-to-gas-station.md
Auto commit by GitBook Editor
1 parent c85830b commit 50c051f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

leetcode/binary-search/774-minimize-max-distance-to-gas-station.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ class Solution {
8888
}
8989
```
9090

91+
```java
92+
class Solution {
93+
public double minmaxGasDist(int[] stations, int K) {
94+
double delta = 1e-6;
95+
double lo = 0, hi = 1e8;
96+
while (hi - lo > delta) {
97+
double mi = (lo + hi) / 2;
98+
if (possible(mi, stations, K)) hi = mi;
99+
else lo = mi;
100+
}
101+
return lo;
102+
}
103+
104+
private boolean possible(double x, int[] stations, int K) {
105+
int count = 0;
106+
for (int i = 1; i < stations.length; i++) {
107+
count += (int) ((stations[i] - stations[i - 1]) / x);
108+
}
109+
return count <= K;
110+
}
111+
}
112+
```
113+
91114
### Additional {#additional}
92115

93116

0 commit comments

Comments
 (0)