-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminimum-time-to-complete-trips.py
66 lines (58 loc) · 2.09 KB
/
minimum-time-to-complete-trips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 2187. Minimum Time to Complete Trips
# 🟠 Medium
#
# https://leetcode.com/problems/minimum-time-to-complete-trips/
#
# Tags: Array - Binary Search
import timeit
from typing import List
# Use binary search to take guesses on the time that it can be needed
# for the n buses to complete k number of trips.
#
# Time complexity: O(n*log(k*t)) - Where n is the number of buses,
# k is the total number of trips to be completed and t is the time
# that the fastest bus needs to complete one trip.
# Space complexity: O(1) - Only pointers used, unless the list
# comprehension takes memory, in that case O(n) but it could be improved
# to O(1) using sum directly instead of the list comprehension.
#
# Runtime 1679 ms Beats 99%
# Memory 28.3 MB Beats 96.75%
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
# The minimum and maximum possible time to complete k trips.
l, r = 0, totalTrips * min(time)
# Binary search the answer.
while l < r:
guess = (l + r) // 2
# Can the buses complete k trips in guess amount of time?
if sum([guess // req for req in time]) >= totalTrips:
# Can we do it faster?
r = guess
else:
# We definitely need more time
l = guess + 1
return l
def test():
executors = [Solution]
tests = [
[[2], 1, 2],
[[1, 2, 3], 5, 3],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.minimumTime(t[0], t[1])
exp = t[2]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()