当前位置:网站首页>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;
}
};
边栏推荐
- Time to calculate cron expression based on cronsequencegenerator
- [learning notes] connectivity and circuit of graph
- Catch all asynchronous artifact completable future
- 03_ Dataimport of Solr
- Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
- Pointer operation - C language
- After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
- ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
- 【学习笔记】图的连通性与回路
- 软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
猜你喜欢
How to choose the appropriate certificate brand when applying for code signing certificate?
What are the advantages and characteristics of SAS interface
Pointer operation - C language
Thymeleaf th:with use of local variables
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
Penetration testing methodology
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
Sharing the 12 most commonly used regular expressions can solve most of your problems
直播预告|如何借助自动化工具落地DevOps(文末福利)
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
随机推荐
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
Section - left closed right open
汇编语言 assembly language
webRTC SDP mslabel lable
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
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
Principle and performance analysis of lepton lossless compression
mysql8.0JSON_ Instructions for using contains
Lepton 无损压缩原理及性能分析
Structure - C language
Sharing the 12 most commonly used regular expressions can solve most of your problems
C语言中限定符的作用
展现强大。这样手机就不会难前进
Matrix chain multiplication dynamic programming example
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
Sorter evolution of ticdc 6.0 principle
03_ Dataimport of Solr
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级