- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2187-minimum-time-to-complete-trips.py
34 lines (27 loc) · 960 Bytes
/
2187-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
# 2187. Minimum Time to Complete Trips
# https://leetcode.com/problems/minimum-time-to-complete-trips
classSolution:
defminimumTime(self, time: list[int], totalTrips: int) ->int:
# Initialize the left and right boundaries.
left, right=1, min(time) *totalTrips
# Can these buses finish 'totalTrips' of trips in 'given_time'?
deftimeEnough(given_time):
returnsum(given_time//tfortintime) >=totalTrips
# Binary search to find the minimum time to finish the task.
whileleft<right:
mid= (left+right) //2
iftimeEnough(mid):
right=mid
else:
left=mid+1
returnleft
# ********************#
# TEST #
# ********************#
importunittest
classTestMinimumTime(unittest.TestCase):
deftest_minimumTime(self):
self.assertEqual(Solution.minimumTime(self, [1, 2, 3, 5], 5), 3)
self.assertEqual(Solution.minimumTime(self, [2], 1),2)
if__name__=='__main__':
unittest.main()