当前位置:网站首页>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;
}
};
边栏推荐
- Use the word "new" to attract curious people
- LeetCode_ 3 (longest substring without repeated characters)
- 微帧科技荣获全球云计算大会“云鼎奖”!
- 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)
- mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
- Thymeleaf th:with use of local variables
- Tdengine biweekly selection of community issues | phase III
- 别不服气。手机功能升级就是强
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
猜你喜欢

Sorter evolution of ticdc 6.0 principle

Introduction, installation, introduction and detailed introduction to postman!

CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion

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

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation

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

分享 12 个最常用的正则表达式,能解决你大部分问题

leetcode:881. 救生艇

Thymeleaf 模板的创建与使用

乌卡时代下,企业供应链管理体系的应对策略
随机推荐
Introduction, installation, introduction and detailed introduction to postman!
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
How to call the function mode of one hand and one machine
矩阵链乘 - 动态规划实例
How to introduce devsecops into enterprises?
mysql8.0JSON_ Instructions for using contains
美国费城发生“安全事故” 2名警察遭枪杀
Why do mechanical engineers I know complain about low wages?
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
乌卡时代下,企业供应链管理体系的应对策略
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
Section - left closed right open
Longest common subsequence dynamic programming
Thymeleaf 常用函数
为什么我认识的机械工程师都抱怨工资低?
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting