当前位置:网站首页>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;
}
};
边栏推荐
- Current situation, trend and view of neural network Internet of things in the future
- Judge whether the variable is an array
- VC development of non MFC program memory leak tracking code
- What are the advantages and characteristics of SAS interface
- R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
- 快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
- How to introduce devsecops into enterprises?
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- Linux下mysql数据库安装教程
猜你喜欢

Introduction, installation, introduction and detailed introduction to postman!

SAS接口有什么优势特点

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

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少

Thymeleaf 使用后台自定义工具类处理文本

What is the future development trend of neural network Internet of things

区间 - 左闭右开

How to deeply understand the design idea of "finite state machine"?

无密码身份验证如何保障用户隐私安全?

如何将 DevSecOps 引入企业?
随机推荐
Tiflash compiler oriented automatic vectorization acceleration
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
分享 12 个最常用的正则表达式,能解决你大部分问题
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
VC开发非MFC程序内存泄漏跟踪代码
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
TDengine 社区问题双周精选 | 第三期
LeetCode_ 3 (longest substring without repeated characters)
R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
Tdengine biweekly selection of community issues | phase III
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
展现强大。这样手机就不会难前进
Current situation, trend and view of neural network Internet of things in the future
【学习笔记】图的连通性与回路
What is the future development trend of neural network Internet of things