possible(D)
that returns True
if we can add k
or fewer gas stations such that the maximum distance between adjacent gas stations is at most D
.X
between stations, calculate the number of gas stations needed as math.floor(X / D)
.k
, return True
; otherwise, return False
.low
and high
for the binary search. low
can be 0
, and high
can be the maximum interval between existing gas stations.low
is less than high
:
a. Calculate mid
as (low + high) / 2
.
b. If possible(mid)
is True
, update high
to mid
.
c. If possible(mid)
is False
, update low
to mid + 1e-6
(to maintain the precision requirement).low
as the smallest possible value of penalty()
.