当前位置:网站首页>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;
}
};
边栏推荐
- 快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
- 汇编语言 assembly language
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
- 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
- R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
- 区间 - 左闭右开
- What is the future development trend of neural network Internet of things
- 怎么叫一手一机的功能方式
- mysql8.0JSON_CONTAINS的使用说明
猜你喜欢

The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million

Thymeleaf th:with局部变量的使用

TiCDC 6.0原理之Sorter演进

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

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

让秒杀狂欢更从容:大促背后的数据库(下篇)

Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success

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

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

网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
随机推荐
Current situation, trend and view of neural network Internet of things in the future
为什么我认识的机械工程师都抱怨工资低?
How does redis implement multiple zones?
无密码身份验证如何保障用户隐私安全?
TiFlash 面向编译器的自动向量化加速
Webrtc learning (II)
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
Fonctions communes de thymeleaf
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
Principle and performance analysis of lepton lossless compression
Loop invariant
【学习笔记】图的连通性与回路
Discussion on memset assignment
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Thymeleaf 使用后台自定义工具类处理文本
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
最简单不用证书也可以多开功能的方式
LeetCode_ 69 (square root of x)
C - Divisors of the Divisors of An Integer Gym - 102040C