当前位置:网站首页>leetcode:881. lifeboat
leetcode:881. lifeboat
2022-07-05 14:28:00 【Oceanstar's study notes】
Title source
Title Description


class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
}
};
title
greedy + Double pointer
(1) Go through the array first , If a person weighs more than limit, Then return to infinity , It means that you can't decide how many ships
(2) Then sort , Slide from the middle dividing point to the left and right
- look for <= limit/2 The rightmost position , As L The pointer
- Find the first one more than limit/2 The location of , As R The pointer
- see L Location and R Can we get a boat together , You can't , exceed

L Move to the left …L Arrived 3, Sure

Don't assign boats first ,R Slide right , Slide all the way to R If you go down one more step, you can't follow 3 Gather up a boat

- The starting point of greed : from 3 Start to the left 6 individual , Go to consume the one on the right 6 individual , It must be the most economical
(3) give an example : The right side is exhausted first 



(4) give an example : The left side is exhausted first 



ship Of Count The amount = × Count The amount 2 + √ Count The amount 2 + Right Side Remnant Next Of Count The amount The number of ships = \frac { × Number } {2} + \frac { √ Number } {2} + The remaining quantity on the right ship Of Count The amount =2× Count The amount +2√ Count The amount + Right Side Remnant Next Of Count The amount
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;
}
};
Double pointer
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;
}
};
边栏推荐
- Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
- How to introduce devsecops into enterprises?
- 03_ Dataimport of Solr
- APR protocol and defense
- Webrtc learning (II)
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- 总量分析 核算方法和势方法 - 分摊分析
- Niuke: intercepting missiles
- PMP考试20天能通过吗?
- 矩阵链乘 - 动态规划实例
猜你喜欢

Introduction, installation, introduction and detailed introduction to postman!

网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理

leetcode:881. 救生艇

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

日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”

周大福践行「百周年承诺」,真诚服务推动绿色环保

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

Topology可视化绘图引擎

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

How to deeply understand the design idea of "finite state machine"?
随机推荐
Catch all asynchronous artifact completable future
C language -- structure and function
循环不变式
[learning notes] stage test 1
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
mysql8.0JSON_CONTAINS的使用说明
Niuke: intercepting missiles
Total amount analysis accounting method and potential method - allocation analysis
Topology可视化绘图引擎
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
3W principle [easy to understand]
Thymeleaf th:with局部变量的使用
Is it OK to open the securities account on the excavation finance? Is it safe?
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
【学习笔记】阶段测试1
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
Strong connection component
无密码身份验证如何保障用户隐私安全?
PMP考试20天能通过吗?