接外包,有相关需求的可以联系我:Telegram | Email

Capacity To Ship Packages Within D Days | Binary Search

该文章创建(更新)于03/2/2022,请注意文章的时效性!

A conveyor belt has packages that must be shipped from one port to another within days days.

The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.

Solution

Binary Search

Code

  • submit code
class Solution:
    def shipWithinDays(self, weights: List[int], days: int) -> int:
        # the sum day under the capacity of content
        def sum_days(weights,content):
            sum_day = 0
            sum_weight = 0
            i = 0
            while i < len(weights):
                sum_weight += weights[i]
                if(sum_weight == content):
                    sum_day += 1
                    sum_weight = 0
                elif(sum_weight <= content and i == len(weights) - 1):
                    sum_day += 1
                elif(sum_weight > content and sum_day <= len(weights)):
                    sum_day += 1
                    sum_weight = 0
                    continue
                elif(weights[i] > content):
                    return 0
                i += 1

            return sum_day

        left = max(weights)
        right = sum(weights)

        while left < right:
            mid = left + int((right - left) / 2)
            temp_sum = sum_days(weights, mid)


            if  temp_sum <= days:
                right = mid
            else:
                left = mid + 1

        return  right
  • full code
class Solution:
    def shipWithinDays(self, weights, days):
        # 在容量为content的情况下运载完需要的总天数
        def sum_days(weights,content):
            sum_day = 0
            sum_weight = 0
            i = 0
            # print("len_weights:" + str(len(weights)))
            while i < len(weights):
                sum_weight += weights[i]
                # print("i: " + str(i) +  "\tsum_day: " + str(sum_day) + "\tsum_weight:\t" + str(sum_weight))
                if(sum_weight == content):
                    sum_day += 1
                    sum_weight = 0
                elif(sum_weight <= content and i == len(weights) - 1):
                    sum_day += 1
                elif(sum_weight > content and sum_day <= len(weights)):
                    sum_day += 1
                    sum_weight = 0
                    continue
                elif(weights[i] > content):
                    return 0
                i += 1

            return sum_day

        left = max(weights)
        # # 最大值为给定的天数和最大的重量
        # # 极端情况下的最大值
        # right = max(weights)  * days
        # left = 1
        # right = max(weights) * len(weights)
        right = sum(weights)
        print("initial right:\t" + str(len(weights)))

        #求取的是最小容量
        while left < right:
            mid = left + int((right - left) / 2)
            print("left:\t" + str(left) + "\tright:\t" + str(right))
            print("mid:\t" + str(mid))
            temp_sum = sum_days(weights, mid)
            print("temp_sum:\t" + str(temp_sum))
            print("------------------------")

            if  temp_sum <= days:
                right = mid
            else:
                left = mid + 1

        return  right


S = Solution()
weights = [1,2,3,4,5,6,7,8,9,10]
days = 5
# 15

# weights = [3,2,2,4,1,4]
# days = 3
# 6

weights = [1,2,3,1,1]
days = 4
# 3

weights = [1,2,3,4,5,6,7,8,9,10]
days  = 1
# 55

print(S.shipWithinDays(weights,days))


👇 Share | 分享 👇


要不赞赏一下?

微信
支付宝
PayPal
Bitcoin

版权声明 | Copyright

除非特别说明,本博客所有作品均采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。转载请注明转自-
https://www.emperinter.info/2022/03/02/capacity-to-ship-packages-within-d-days-binary-search/


要不聊聊?

我相信你准备留下的内容是经过思考的!【勾选防爬虫,未勾选无法留言】

*

*



微信公众号

👉 NewsLetter ❤️ 邮箱订阅 👈

优惠码