1011. Capacity To Ship Packages Within D Days

摘要:
AconveyorbelthaspackagesthatmustbeshippedfromoneporttoanotherwithinDdays.Thei-thpackageontheconveyorbelthasaweightofweights[i].Eachday,weloadtheshipwithpackagesontheconveyorbelt(intheordergivenbyweigh

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

Thei-th package on the conveyor belt has a weight ofweights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given byweights). 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 withinDdays.

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

Note:

  1. 1 <= D <= weights.length <= 50000
  2. 1 <= weights[i] <= 500

跟上面那道题很像,是真的像

同样是二分法,先确认边界,left应该是weights的最大值,right应该是weights的sum

while(left < right)

medium=1/2(left+right)乃当前capacity

设置local变量day用来计算当前capacity最多用几天,再设置cur用来存放此时的load量

for each w 循环weights

如果need加w大于了medium,说明day要+1,重置cur

cur += w

最后判断一下day和D的关系,大于说明要的天数太多了得减少,通过增加capacity(left = mid + 1)来实现,否则right = mid

return left

classSolution {
  public int shipWithinDays(int[] weights, intD) {
        int left = 0, right = 0;
        for (intw: weights) {
            left =Math.max(left, w);
            right +=w;
        }
        while (left <right) {
            int mid = (left + right) / 2, day = 1, cur = 0;
            for (intw: weights) {
                if (cur + w >mid) {
                    day += 1;
                    cur = 0;
                }
                cur +=w;
            }
            if (day > D) left = mid + 1;
            else right =mid;
        }
        returnleft;
    }
}

免责声明:文章转载自《1011. Capacity To Ship Packages Within D Days》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Postman使用详解MATLAB中导入数据:importdata函数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Jenkins安装

1、 Jenkins简介1.开源自动化持续集成和部署平台CI、持续集成CD和持续部署2.Jenkins Free风格任务管道Maven项目多配置项目多分支管道任务支持的任务类型,不会执行任何更新;触发器(由Gitlab...

SpringBoot工程通过Maven引入自定义Jar包

A工程为:common工程打成jar包:common-0.0.1-SNAPSHOT.jar注意:A工程打包时要使用maven的插件进行打包,不然会打成SpringBoot的Jar包,无法使用。--字符集编码--˃打包时跳过测试配置1.8˂!...

记一次Arcgis Server10.2许可过期导致发布图层失败

1.今天,当使用arcmap将地图服务发布到arcgisserver时,发布突然失败。在arcgisserver的管理页面的日志选项中发现错误:未能初始化服务器对象“System/PublicingTools”:0x80004005:错误:(-8003)YourArcGISServerlicense已过期。2.然后在服务器路径中查找文件:...

2.页面绘制和引入组件库uView

文本+背景色的形式,而不是横幅图的形式,可以节省未来的工作量。在index.vue中,关于开关的代码:EFGHIJKLMNOPQRSTUWXYZB˃DEFGHIJKLNNOPQRSTUVWXYZEFGHIJKLMNOPQRSTUVWXYZ导出默认值{data(){return{}},onLoad()},方法:{}}。横幅{width:100%;height:...

Win10阻止电脑关机时弹出正在关闭应用的方法及恢复

当计算机上安装了Windows 10时,当我们在未完成所有运行程序的情况下关闭计算机时,将弹出n个应用程序正在关闭并关闭的提示。第一步是创建一个自动关闭应用程序并将其添加到注册表的注册表文件。这可以解决在Windows 10系统计算机关闭时弹出提示关闭n个应用程序并关闭的问题。...

Docker(一)

Docker的优势:1.更高效的利用系统资源。docker-v:查看Docker版本。dockerhistory:查看镜像内的历史记录。dockerdiff:查看修改的内容。使用Dockerfile定制镜像:1.以之前定制nginx镜像为例,这次我们使用Dockerfile来定制。操作Docker容器:启动容器有两种方式:一种:是基于镜像新建一个容器并启动,...