Skip to content

Commit 3bc48fb

Browse files
authored
Update minimize-max-distance-to-gas-station.py
1 parent 706561e commit 3bc48fb

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Python/minimize-max-distance-to-gas-station.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# Time: O(nlogr)
22
# Space: O(1)
33

4+
import math
5+
6+
47
class Solution(object):
58
def minmaxGasDist(self, stations, K):
69
"""
710
:type stations: List[int]
811
:type K: int
912
:rtype: float
1013
"""
11-
def possible(stations, K, guess):
12-
return sum(int((stations[i+1]-stations[i]) / guess)
13-
for i in xrange(len(stations)-1)) <= K
14+
def check(x):
15+
return sum(int(math.ceil((stations[i+1]-stations[i])/x))-1 for i in xrange(len(stations)-1)) <= K
1416

15-
left, right = 0, 10**8
17+
left, right = 0, stations[-1]-stations[0]
1618
while right-left > 1e-6:
1719
mid = left + (right-left)/2.0
18-
if possible(mid):
20+
if check(mid):
1921
right = mid
2022
else:
2123
left = mid

0 commit comments

Comments
 (0)