当前位置:网站首页>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;
}
};
边栏推荐
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- 一网打尽异步神器CompletableFuture
- R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
- 开挖财上的证券账户可以吗?安全吗?
- C - Divisors of the Divisors of An Integer Gym - 102040C
- Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
- 享你所想。智创未来
- 汇编语言 assembly language
- [learning notes] connectivity and circuit of graph
- Use the word "new" to attract curious people
猜你喜欢

魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问

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

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

Sharing the 12 most commonly used regular expressions can solve most of your problems

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

Lepton 无损压缩原理及性能分析

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

Thymeleaf th:with局部变量的使用

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】

家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
随机推荐
魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
强联通分量
Show strength. In this way, the mobile phone will not be difficult to move forward
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
Postgresql 13 安装
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
汇编语言 assembly language
Penetration testing methodology
The simplest way to open more functions without certificates
mysql8.0JSON_CONTAINS的使用说明
VC development of non MFC program memory leak tracking code
Section - left closed right open
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
非技术部门,如何参与 DevOps?
webRTC SDP mslabel lable
LeetCode_ 2 (add two numbers)
Mysql database installation tutorial under Linux