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
| class Solution: def shipWithinDays(self, weights: List[int], days: int) -> int: left, right = max(weights)-1, 500*len(weights)+1
def check(weights: List[int], ship_load: int, days: int) -> bool: if sum(weights) > ship_load*days: return False sum_num = 0 use_days = 0 for i in weights: sum_num += i if sum_num > ship_load: sum_num = i use_days += 1 use_days += 1 if sum_num > 0 else 0
return use_days <= days while left+1 < right: mid = (left + right) // 2 if check(weights, mid, days): right = mid else: left = mid return right
|