当前位置:网站首页>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;
}
};
边栏推荐
- 做自媒体视频二次剪辑,怎样剪辑不算侵权
- 分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
- 3W principle [easy to understand]
- Linux下mysql数据库安装教程
- 魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
- Longest common subsequence dynamic programming
- 详解Vue适时清理keepalive缓存方案
- 美国费城发生“安全事故” 2名警察遭枪杀
- ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
- Thymeleaf common functions
猜你喜欢

Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares

Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)

为什么我认识的机械工程师都抱怨工资低?

世界环境日 | 周大福用心服务推动减碳环保

Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products

基于 TiDB 场景式技术架构过程 - 理论篇

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

Thymeleaf 模板的创建与使用

Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
随机推荐
Thymeleaf 模板的创建与使用
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
How to make a second clip of our media video without infringement
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
Redis如何实现多可用区?
做自媒体视频二次剪辑,怎样剪辑不算侵权
微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
Fonctions communes de thymeleaf
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
Thymeleaf 使用后台自定义工具类处理文本
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
Thymeleaf common functions
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection