当前位置:网站首页>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 使用后台自定义工具类处理文本
- PMP考试20天能通过吗?
- 2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
- How to introduce devsecops into enterprises?
- C language -- structure and function
- Is it OK to open the securities account on the excavation finance? Is it safe?
- Pointer operation - C language
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
- Principle and performance analysis of lepton lossless compression
猜你喜欢

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

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

PHP - fatal error: allowed memory size of 314572800 bytes exhausted

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

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)

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

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

How to introduce devsecops into enterprises?

Topology可视化绘图引擎

Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu
随机推荐
Topology可视化绘图引擎
Longest common subsequence dynamic programming
The simplest way to open more functions without certificates
Niuke: intercepting missiles
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
Introduction, installation, introduction and detailed introduction to postman!
Disjoint Set
Don't be unconvinced. Mobile phone function upgrade is strong
Section - left closed right open
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
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
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
Mysql database installation tutorial under Linux
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Matrix chain multiplication dynamic programming example
Webrtc learning (II)