Minimum Time to Complete Trips — LeetCode 2187 Python Solution

MediumArrayBinary Search
Problem
#2187
Reading time
2 min

The problem

You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip. Each bus can make multiple trips successively; that is, the next trip can start immediately after completing the current trip.

Example

Input
time = [1,2,3], totalTrips = 5
Output
3
Explanation
- At time t = 1, the number of trips completed by each bus are [1,0,0].

Python solution

Python
class Solution:
    def minimumTime(self, time: List[int], totalTrips: int) -> int:
        mx = min(time) * totalTrips
        return bisect_left(
            range(mx), totalTrips, key=lambda x: sum(x // v for v in time)
        )

Complexity

MeasureComplexity
TimeO(n \times \log(m \times k)), where n and k are the length of the array time and totalTrips respectively, and m is the minimum value in the array time
SpaceO(1) auxiliary

Pattern: Monotonic Stack

Answer "what is the next greater element" for every position in one pass. LeetCode 2187. Minimum Time to Complete Trips is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.

The monotonic stack guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2187. Minimum Time to Complete Trips?
LeetCode 2187. Minimum Time to Complete Trips is rated Medium on LeetCode.
What is the time complexity of LeetCode 2187. Minimum Time to Complete Trips?
The Python solution on this page runs in O(n \times \log(m \times k)), where n and k are the length of the array time and totalTrips respectively, and m is the minimum value in the array time.
What is the space complexity of LeetCode 2187. Minimum Time to Complete Trips?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2187. Minimum Time to Complete Trips cover?
LeetCode 2187. Minimum Time to Complete Trips is tagged Array and Binary Search on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview