Leetcode 1011:在 D 天内送达包裹的能力 Capacity To Ship Packages Within D Days

中文描述:

传送带上的包裹必须在 D 天内从一个港口运送到另一个港口。
传送带上的第 i 个包裹的重量为 weights[i]。每一天,我们都会按给出重量的顺序往传送带上装载包裹。我们装载的重量不会超过船的最大运载重量。
返回能在 D 天内将传送带上的所有包裹送达的船的最低运载能力。

题目描述:

A conveyor belt has packages that must be shipped from one port to another within D 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 D days.

Example 1:

Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
Output: 15
Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10

Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.

Example 2:

Input: weights = [3,2,2,4,1,4], D = 3
Output: 6
Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4

Example 3:

Input: weights = [1,2,3,1,1], D = 4
Output: 3
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1

Constraints:

1 <= D <= weights.length <= 5 * 104
1 <= weights[i] <= 500

Time complexity: O O O( N N Nlog(W)), where N is the number of piles, and W is the maximum size of a pile.
Space complexity: O O O( 1 1 1)
二分 二分搜索总结
根据上面的二分搜索总结,注意此题为找最低运载能力 使得船能在 D 天内将传送带上的所有包裹送达。
则此题可以归纳为二分找左边界(最优区间的左边) 套用基础模版:
初始化左边界 lo:最少只装载一个包裹的重量则为:左右包裹中重量的最大值。
初始化右边界 hi:最多一次将所有的包裹都装载:所有包裹重量的和。
循环条件: lo < hi
中间位置 mid = lo + (hi-lo>>1),
更新左边界 lo = mid+1,
更新右边界 hi = mid,
左右边界的判断条件:
注意更新左右边界的判断条件当我们给定一个值 capacity代表运载能力 ,我们可以线性地验证是否存在一种方案,当运载能力为 capacity时, 所有包裹仍然能在 D天内运完。

贪心地模拟分割的过程,从前到后遍历数组,用 time 表示当前所用的时间总和,cur 表示当前还未运送的货物的重量。当 cur + w(当前包裹的重量) > capacity 时表示 此时货物不能一天内送完, 要先将之前剩下的运送完 之后 再运送当前的,即:time ++。 同时更新当前未运送的货物的重量 cur = w。遍历结束后验证是否 time 不超过 D。

满足此条件的话则我们在 [lo, x(mid)] 的 范围内继续寻找是否存在更小的 x 值满足此条件 即 hi = mid。反之则表示给定的 速度x值过小 lo = mid + 1.

class Solution {
    public int shipWithinDays(int[] weights, int D) {
        int lo = 0;
        int hi = 0;
        for(int w:weights){
            lo = Math.max(lo, w);
            hi += w;
        }
        int mid;
        while(lo < hi){
            mid = lo + (hi - lo>>1);
            if(check(weights, D, mid)){
                // satisfy mid capa ship in D days;
                hi = mid;
            }else{
                lo = mid+1;
            }
        }
        return lo;
    }
    
    private boolean check(int[] weights, int D, int capacity){
        int time = 1;
        int cur = 0;
        for(int w:weights){
            if (cur + w > capacity) {
                time++;
                cur = 0;
            }
            cur += w;
        }
        return time <= D;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值