선발대

[스파르타] 99클럽 2기 코테스터디 23일차 TIL / Leet 1011 본문

스파르타코딩클럽/활동 내용

[스파르타] 99클럽 2기 코테스터디 23일차 TIL / Leet 1011

신선한 스타트 2024. 6. 11. 17:23

LeetCode: 1011. Capacity To Ship Packages Within D Days (링크)

 

1011. Capacity To Ship Packages Within D Days

 

 

class Solution:
    def shipWithinDays(self, weights: List[int], days: int) -> int:
        l, r = max(weights), sum(weights)
        res = r

        def canShip(cap):
            ships, currCap = 1, cap
            for w in weights:
                if currCap - w < 0:
                    ships += 1
                    currCap = cap
                currCap -= w
            return ships <= days

        while l <= r:
            cap = (l + r) // 2
            if canShip(cap):
                res = min(res, cap)
                r = cap - 1
            else:
                l = cap + 1
        return res

오늘 미들러 풀이 끝!


참고한 풀이: https://www.youtube.com/watch?v=ER_oLmdc-nw

Comments