[LeetCode] 881. Boats to Save People 渡人的船
The `i`-th person has weight `people[i]`, and each boat can carry a maximum weight of `limit`.
Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit
.
Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.)
Example 1:
Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)
Example 2:
Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)
Example 3:
Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)
Note:
1 <= people.length <= 50000
1 <= people[i] <= limit <= 30000
这道题让我们载人过河,说是每个人的体重不同,每条船承重有个限度 limit(限定了这个载重大于等于最重人的体重),同时要求每条船不能超过两人,这尼玛是独木舟吧,也就比 kayak 大一点点吧(不过也有可能是公园湖中的双人脚蹬船,怀念小时候在公园划船的日子~),问我们将所有人载到对岸最少需要多少条船。从题目中的例子2可以看出,最肥的人有可能一人占一条船,当然如果船的载量够大的话,可能还能挤上一个瘦子,那么最瘦的人是最可能挤上去的,所以策略就是胖子加瘦子的上船组合。那么这就是典型的贪婪算法的适用场景啊,首先要给所有人按体重排个序,从瘦子到胖子,这样我们才能快速的知道当前最重和最轻的人。然后使用双指针,left 指向最瘦的人,right 指向最胖的人,当 left 小于等于 right 的时候,进行 while 循环。在循环中,胖子是一定要上船的,所以 right 自减1是肯定有的,但是还是要看能否再带上一个瘦子,能的话 left 自增1。然后结果 res 一定要自增1,因为每次都要用一条船,参见代码如下:
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
int res = 0, n = people.size(), left = 0, right = n - 1;
sort(people.begin(), people.end());
while (left <= right) {
if (people[left] + people[right] <= limit) ++left;
--right;
++res;
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/881
参考资料:
https://leetcode.com/problems/boats-to-save-people/
https://leetcode.com/problems/boats-to-save-people/discuss/156740/C%2B%2BJavaPython-Two-Pointers
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 881. Boats to Save People 渡人的船的更多相关文章
- LeetCode 881. Boats to Save People
原题链接在这里:https://leetcode.com/problems/boats-to-save-people/ 题目: The i-th person has weight people[i] ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- LC 881. Boats to Save People
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat c ...
- 881. Boats to Save People
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat c ...
- [Leetcode 881]船救人 Boats to Save People 贪心
[题目] The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each b ...
- 【leetcode】885. Boats to Save People
题目如下: 解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近limit.考虑到人的总数最大是50000,而每个人的体重最大是30 ...
- [Swift]LeetCode881. 救生艇 | Boats to Save People
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat c ...
- LeetCode 881.救生艇(C++)
第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- maven打包时生成源代码
<build> <finalName>${artifactId}</finalName> <plugins> <plugin> <ar ...
- 海边拾贝-C-面试篇
优秀的面试资料,不定期会更新: Leetcode上面别人整理的若干面试资料: https://github.com/huihut/interview 剑指offer:https://blog.csdn ...
- Jmockit 构造函数与初始代码块
from Jmockit 中文网 http://jmockit.cn/showArticle.htm?channel=4&id=14 有些编写不良好的类的构造函数,初始代码块,静态代码块存在大 ...
- linux 修改文件的时间属性
二.修改文件时间 创建文件我们可以通过touch来创建.同样,我们也可以使用touch来修改文件时间.touch的相关参数如下: -a : 仅修改access time. -c : 仅修改时间,而不建 ...
- DVWA-基于布尔值的盲注与基于时间的盲注学习笔记
DVWA-基于布尔值的盲注与基于时间的盲注学习笔记 基于布尔值的盲注 一.DVWA分析 将DVWA的级别设置为low 1.分析源码,可以看到对参数没有做任何过滤,但对sql语句查询的返回的结果做了改变 ...
- Linux文本文件——管理文本的命令
Linux文本文件——管理文本的命令 摘要:本文主要学习了在Linux中管理文本的命令. cat命令 cat命令用来显示文本文件的内容,也可以把几个文件内容附加到另一个文件中,即连接合并文件,是Con ...
- Ext.create使用(下)
本文介绍第三种使用方法: //通过类的引用实例化一个类 var w1 = Ext.create(Ext.window.Window, {//类的引用 title: '窗体', html:'<fo ...
- 电信NBIOT 5 - NB73模块下行测试(自己平台-电线平台-NB73)
电信NBIOT 1 - 数据上行(中国电信开发者平台对接流程) 电信NBIOT 2 - 数据上行(中间件获取电信消息通知) 电信NBIOT 3 - 数据下行 电信NBIOT 4 - NB73模块上行测 ...
- 程序员必备技能之Markdown
Markdown介绍 Markdown是一种纯文本格式的标记语言,比HTML更简单,通过一些简单的语法标记,就可以让文本简洁好看. Markdown已经是程序员一项必备技能了,代码块.流程图.序列图. ...
- HeadFirst设计模式---观察者
表达公式 注册者 + 订阅者 = 观察者模式 设计气象站 气象站接口 /** ** 布告板 ** @author lollipop ** @since 2019/11/24 **/ public in ...