当前位置:网站首页>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;
}
};
边栏推荐
- 做自媒体视频二次剪辑,怎样剪辑不算侵权
- 微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
- 动态规划
- 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
- Sorter evolution of ticdc 6.0 principle
- Tiflash compiler oriented automatic vectorization acceleration
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- 日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
- Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
猜你喜欢
LeetCode_ 2 (add two numbers)
How to protect user privacy without password authentication?
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
Scenario based technology architecture process based on tidb - Theory
SAS接口有什么优势特点
Countermeasures of enterprise supply chain management system in UCA Era
如何深入理解“有限状态机”的设计思想?
Thymeleaf 使用后台自定义工具类处理文本
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
随机推荐
周大福践行「百周年承诺」,真诚服务推动绿色环保
如何将 DevSecOps 引入企业?
C - Divisors of the Divisors of An Integer Gym - 102040C
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
乌卡时代下,企业供应链管理体系的应对策略
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
mysql8.0JSON_CONTAINS的使用说明
Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
C - Divisors of the Divisors of An Integer Gym - 102040C
享你所想。智创未来
判断变量是否为数组
Lepton 无损压缩原理及性能分析
Linux下mysql数据库安装教程
04_ Use of solrj7.3 of solr7.3
What category does the Internet of things application technology major belong to
03_ Dataimport of Solr
Thymeleaf 常用函數
How does redis implement multiple zones?
TiFlash 面向编译器的自动向量化加速
SAS接口有什么优势特点