二分搜索

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

 

相关文章:

  • 2022-01-26
  • 2022-12-23
  • 2021-06-18
  • 2022-02-04
  • 2021-09-07
  • 2022-12-23
  • 2021-07-30
  • 2022-01-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2021-11-12
  • 2022-12-23
相关资源
相似解决方案