当前位置:网站首页>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;
}
};
边栏推荐
- Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
- Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
- 怎么叫一手一机的功能方式
- CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
- The simplest way to open more functions without certificates
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- [learning notes] stage test 1
- 不相交集
- Niuke: intercepting missiles
- How to choose the appropriate certificate brand when applying for code signing certificate?
猜你喜欢

ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)

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

SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain

Section - left closed right open

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

World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection

Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module

如何深入理解“有限状态机”的设计思想?

【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)

Share 20 strange JS expressions and see how many correct answers you can get
随机推荐
【学习笔记】阶段测试1
Time to calculate cron expression based on cronsequencegenerator
Use the word "new" to attract curious people
PMP考试20天能通过吗?
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
Thymeleaf common functions
How to make a second clip of our media video without infringement
Introduction, installation, introduction and detailed introduction to postman!
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
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
Scenario based technology architecture process based on tidb - Theory
矩阵链乘 - 动态规划实例
强联通分量
[learning notes] stage test 1
做自媒體視頻二次剪輯,怎樣剪輯不算侵權
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
The function of qualifier in C language
分享 12 个最常用的正则表达式,能解决你大部分问题
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
不相交集