当前位置:网站首页>[two points] leetcode1011 Capacity To Ship Packages Within D Days
[two points] leetcode1011 Capacity To Ship Packages Within D Days
2022-06-23 03:41:00 【Twilight_ years】
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.
The question : Divide an array into... At most days Share ( continuity ), Sum each group The minimum of the maximum .

Ideas : greedy + Two points answer
Suppose that the carrying capacity of the ship is x when , Can be in days Deliver all packages within days , So as long as the carrying capacity is greater than x, You can also days Deliver all packages within days .
Conclusion :
There is a common carrying capacity 【 Lower limit 】x0, When x>=x0 when , Can be in days Deliver all packages within days , When x<x0 when , Cannot be in days Deliver all packages within days .
therefore ,x0 Is the answer that needs to be found .
You can use the binary search method to find x0.
Determine the problem : Given carrying capacity x, Can it be in days All the packages will be delivered within days ?
Solve by greedy means :
Because you must follow the array Weights In the order of the packages , So we start with an array weights Start traversing the first element of , Arrange for successive packages to be delivered on the same day . When the weight of this parcel is greater than the carrying capacity x when , You need to take out the last package , On a new day , And continue to traverse . When we traverse the entire array , You get the minimum number of days to transport .
Compare the minimum number of days to be shipped with days Compare , And then we can solve this problem .
Be careful check The logic of :
class Solution {
public int shipWithinDays(int[] weights, int days) {
int l=1,r=500*50000;
while(l<r){
int mid=(l+r)/2;
int need=1,cur=0;
for(int w:weights){
if(w>mid)need=(int)1e6;
if(cur+w>mid){
need++;
cur=0;
}
cur+=w;
}
if(need<=days){
r=mid;
}else l=mid+1;
}
return l;
}
}边栏推荐
- Official announcement! The Hong Kong Zhuhai Macao Bridge is finally here!
- Tcapulusdb Jun · industry news collection (IV)
- Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
- Record an edusrc vulnerability mining
- Detailed discussion on modular architecture design of MCU firmware
- How to batch generate jan13 barcode
- Firewall and IP security policy configuration
- How to store easydss version 3.0 video files in other free disks?
- I Arouter framework analysis
- Preliminary sequencing problem
猜你喜欢

Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)

The first batch of job hunting after 00: don't misread their "different"

【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行

Google Earth engine (GEE) - long time series monthly VCI data extraction, analysis and area calculation (Mexico as an example)

JS Part 4

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

1-1VMware介绍

选择排序法

mysql 数据恢复 (.ibdata1, bin log)
随机推荐
Using jhipster to build microservice architecture
The power of code refactoring: how to measure the success of refactoring
聊聊内存模型和内存序
SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
Brief introduction to arm architecture
Generate PDF417 code in batch through TXT file
How to configure the domain name with low code of micro build
【LeetCode】两数之和II
Not just offline caching- On how to make good use of serviceworker
TRTC setaudioroute invalid problem
What is the potential of dmail based on Web3.0? First round financing of $10 million?
【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
How to save the model obtained from sklearn training? Just read this one
The first batch of job hunting after 00: don't misread their "different"
What is the difference between ArrayList and array?
1058 multiple choice questions (20 points)
Golang resource embedding scheme
Navar's Treasure Book: the principle of getting rich without luck
Parsing the implementation of easygbs compatible token as parameter passing
How to store easydss version 3.0 video files in other free disks?