LeetCode 881. Boats to Save People
原题链接在这里:https://leetcode.com/problems/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 <= 500001 <= people[i] <= limit <= 30000
题解:
When trying to find out minimum num ber of boats to carry every given person, it is best to sort array first and let heaviest person and lightest person fit the boat first.
If it is overweight, then only let the heaviest person take and boat, res++.
Otherwise, let both of them take the boat, and since boat could carry at most 2 people at the same time, move both pointers, res++.
Time Complexity: O(nlogn). n = people.length.
Space: O(1).
AC Java:
class Solution {
public int numRescueBoats(int[] people, int limit) {
if(people == null || people.length == 0){
return 0;
}
Arrays.sort(people);
int res = 0;
int i = 0;
int j = people.length-1;
while(i<=j){
if(people[i]+people[j]<=limit){
i++;
j--;
}else{
j--;
}
res++;
}
return res;
}
}
LeetCode 881. Boats to Save People的更多相关文章
- [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 c ...
- 【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 ...
随机推荐
- Symfony框架笔记
控制器获取前端传入的参数 $parameters = $request->request; $data = $parameters->all(); 引用静态资源(js.css.img) 使 ...
- DELPHI6中DSGNINTF.DCU找不到时的解决方法
https://www.cnblogs.com/gaodu2003/archive/2009/06/04/1495789.html 1.添加 lib\designide.dcp到控件的dpk文件的re ...
- 如何同时读取 TDateTimePicker 的 Date 和 Time ?
由于 TDateTimePicker 只能用于日期或时间,不能同时使用.如果将Kind属性设置为dtkDate,则可以指定自定义DATE格式,但忽略任何TIME格式,并且Time未定义使用该属性.如果 ...
- Python之路【第二十二篇】:轮播图片CSS
轮播代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- zookeeper从入门到精通视频教程(含网盘下载地址)
Zookeeper视频教程链接:https://pan.baidu.com/s/1V9YZN5F3sTKQJOhiDt9hnA 提取码:rtgl
- UI单据按钮点击事件校验
一.按钮点击前事务处理<BeforeEventProcess> public override void BeforeEventProcess(IPart part, string eve ...
- php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本
php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本 注意: 01) imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用 puten ...
- [洛谷P4385][COCI2009]Dvapravca(咕咕咕)
题目大意:很早以前做的题 题解: 卡点: C++ Code: #pragma GCC optimize("Ofast") #pragma GCC optimize("un ...
- TCP协议学习笔记
TCP协议数据格式 TCP协议在互联网ISO协议的传输层. 在互联网传输过程中,互联网包在数据链路层,是传输数据的最基础的包.一个互联网的包包含IP包,即互联网包 = 互联网信息包头(至少20字节)+ ...
- 分享AWS网站
1.AWS服务运行状况检测网站: https://status.amazonaws.cn/ 2.AWS架构白皮书:https://aws.amazon.com/cn/architecture/?a ...