二分搜索
class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: List[int] :type D: int :rtype: int """ l, r, middle = max(weights), sum(weights), 0 # start the binary search strategy while l < r: middle = int((l + r) / 2) # check whether this capacity(middle value) is best days, w = 1, 0 for i, val in enumerate(weights): w += val if w > middle: days += 1 w = val if days > D: l = middle + 1 if days <= D: r = middle return l