当前位置:网站首页>leetcode:881. 救生艇
leetcode:881. 救生艇
2022-07-05 14:20:00 【OceanStar的学习笔记】
题目来源
题目描述


class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
}
};
题目解析
贪心+双指针
(1)先遍历一遍数组,如果有一个人的体重超过了limit,那么返回无穷大,表示多少条船都搞不定
(2)然后排序,从中间分界点开始往左右两边滑
- 找 <= limit/2最右边的位置,作为L指针
- 找第一个超过limit/2的位置,作为R指针
- 看L位置和R位置能否凑一个船,不能,超过

L往左边移动…L来到了3,可以

先不分配船,R往右滑,一直滑到R再往下进一个就没有办法跟3凑一船为止

- 贪心的出发点:从3出发往左数6个,去消耗右边的这6个,一定是最省的
(3)举例:右边先耗尽



(4)举例:左侧先耗尽



船 的 数 量 = × 数 量 2 + √ 数 量 2 + 右 侧 剩 下 的 数 量 船的数量 = \frac { ×数量} {2} + \frac { √数量} {2} + 右侧剩下的数量 船的数量=2×数量+2√数量+右侧剩下的数量
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;
}
};
首尾双指针
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;
}
};
边栏推荐
- LeetCode_ 2 (add two numbers)
- 基于伯努利原理的速度监测芯片可用于天然气管道泄露检测
- POI set the data format of the column (valid)
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
- 03_Solr之dataimport
- Sorter evolution of ticdc 6.0 principle
- How to make a second clip of our media video without infringement
- Discussion on memset assignment
- Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
- R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
猜你喜欢

Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management

CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕

Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)

如何将 DevSecOps 引入企业?

TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析

区间 - 左闭右开

ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)

乌卡时代下,企业供应链管理体系的应对策略

CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion

家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
随机推荐
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
判断变量是否为数组
01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
Tiflash compiler oriented automatic vectorization acceleration
Disjoint Set
Enjoy what you want. Zhichuang future
What is the future development trend of neural network Internet of things
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
Thymeleaf th:with use of local variables
Time to calculate cron expression based on cronsequencegenerator
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
Thymeleaf common functions
mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
How does redis implement multiple zones?
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
分享 12 个最常用的正则表达式,能解决你大部分问题
Use the word "new" to attract curious people