[LeetCode] 1011. Capacity To Ship Packages Within D Days

김민우·2023년 2월 22일
0

알고리즘

목록 보기
147/189

- Problem

1011. Capacity To Ship Packages Within D Days


- 내 풀이 (이분탐색)

class Solution:
    def shipWithinDays(self, weights: List[int], days: int) -> int:
        left, right = max(weights), 25000000 # 500 * 5 * (10 ** 4)

        while left <= right:
            mid = (left + right) // 2
            capacity = mid
            day = 1

            for weight in weights:
                if capacity - weight < 0:
                    capacity = mid
                    day += 1

                capacity -= weight
                
            if day > days:
                left = mid + 1
            
            else:
                right = mid - 1
        
        return left

- 결과

profile
Pay it forward.

0개의 댓글