파비의 매일매일 공부기록

2023.03.07 Today's Challenge 본문

Problem Solving/LeetCode

2023.03.07 Today's Challenge

fabichoi 2023. 3. 7. 23:45

https://leetcode.com/problems/minimum-time-to-complete-trips/

 

Minimum Time to Complete Trips - LeetCode

Can you solve this real interview question? Minimum Time to Complete Trips - 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 sta

leetcode.com

이분 탐색으로 풀면 됨.

class Solution:
    def minimumTime(self, time: List[int], totalTrips: int) -> int:
        l, r = 1, max(time) * totalTrips

        def time_enough(tt):
            trips = 0
            for t in time:
                trips += tt // t
            return trips >= totalTrips

        while l < r:
            mid = (l + r) // 2
            if time_enough(mid):
                r = mid
            else:
                l = mid + 1
        
        return l
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.03.09 Today's Challenge  (0) 2023.03.09
2023.03.08 Today's Challenge  (0) 2023.03.08
2023.03.06 Today's Challenge  (0) 2023.03.06
2023.03.05 Today's Challenge  (0) 2023.03.05
2023.03.04 Today's Challenge  (0) 2023.03.04
Comments