当前位置:网站首页>leetcode:881. lifeboat
leetcode:881. lifeboat
2022-07-05 14:28:00 【Oceanstar's study notes】
Title source
Title Description
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
}
};
title
greedy + Double pointer
(1) Go through the array first , If a person weighs more than limit, Then return to infinity , It means that you can't decide how many ships
(2) Then sort , Slide from the middle dividing point to the left and right
- look for <= limit/2 The rightmost position , As L The pointer
- Find the first one more than limit/2 The location of , As R The pointer
- see L Location and R Can we get a boat together , You can't , exceed
L Move to the left …L Arrived 3, Sure
Don't assign boats first ,R Slide right , Slide all the way to R If you go down one more step, you can't follow 3 Gather up a boat
- The starting point of greed : from 3 Start to the left 6 individual , Go to consume the one on the right 6 individual , It must be the most economical
(3) give an example : The right side is exhausted first
(4) give an example : The left side is exhausted first
ship Of Count The amount = × Count The amount 2 + √ Count The amount 2 + Right Side Remnant Next Of Count The amount The number of ships = \frac { × Number } {2} + \frac { √ Number } {2} + The remaining quantity on the right ship Of Count The amount =2× Count The amount +2√ Count The amount + Right Side Remnant Next Of Count The amount
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
if(people.empty()){
return 0;
}
int N = people.size();
std::sort(people.begin(), people.end());
if(people[N - 1] > limit){
return -1;
}
int lessR = -1;
for (int i = N - 1; i >= 0; --i) {
if(people[i] <= limit/2){
lessR = i;
break;
}
}
if(lessR == -1){
return N;
}
int L = lessR;
int R = lessR + 1;
int noUsed = 0;
while (L >= 0){
int solved = 0;
while (R < N && people[L] + people[R] <= limit){
solved++;
R++;
}
if(solved == 0){
noUsed++;
L--;
}else{
L = std::max(-1, L - solved);
}
}
int all = lessR + 1;
int used = all - noUsed;
int moreUnsolved = (N - all) - used;
return used + ((noUsed + 1) >> 1) + moreUnsolved;
}
};
Double pointer
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
int N = people.size();
std::sort(people.begin(), people.end());
if(people[N - 1] > limit){
return -1;
}
int ans = 0, l = 0, r = N - 1, sum = 0;
while (l <= r){
sum = l == r ? people[l] : people[l] + people[r];
if(sum > limit){
r--;
}else{
l++;
r--;
}
ans++;
}
return ans;
}
};
边栏推荐
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
- 3W principle [easy to understand]
- 快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
- 区间 - 左闭右开
- Catch all asynchronous artifact completable future
- leetcode:881. 救生艇
- Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
- dynamic programming
- SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
猜你喜欢
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
Loop invariant
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
Redis如何实现多可用区?
区间 - 左闭右开
Tdengine biweekly selection of community issues | phase III
Section - left closed right open
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
随机推荐
04_ Use of solrj7.3 of solr7.3
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
Postman简介、安装、入门使用方法详细攻略!
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
C language -- structure and function
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
Show strength. In this way, the mobile phone will not be difficult to move forward
微帧科技荣获全球云计算大会“云鼎奖”!
mysql8.0JSON_ Instructions for using contains
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
为什么我认识的机械工程师都抱怨工资低?
mysql8.0JSON_CONTAINS的使用说明
Strong connection component
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
做自媒体视频二次剪辑,怎样剪辑不算侵权
webRTC SDP mslabel lable