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 <= 50000
1 <= 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 ...
随机推荐
- Sqlite清空表数据以及重新设置主键操作
Sqlite清空表数据以及重新设置主键操作 delete from 表名; //清空数据 update sqlite_sequence SET seq = 0 where name ='表名';//自 ...
- 英语insuraunce保险
中文名:保险 外文名:insurance或insuraunce 类型:保障机制,商业行为 作用:资金融通.损失补偿等 原则:分摊.代位.大数法则等原则 性质:契约经济关系 意义:市场经济条件下风险管理 ...
- C#:蓝牙串口读数据和写数据
首次使用C#编写与COM口有关的程序,期间遇到了很多问题,写下自己的经验总结,如有错漏,欢迎批评指正! 1.新建一个串口类( SerialPort类) //Create a serial port f ...
- lsyncd实时同步工具
简介 Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实 ...
- js获取对象的属性个数
for (var i = 0; i < dt.length; i++) { if (Object.keys(dt[i]).length <= 1) { dt.splice(i, 1); i ...
- sparksql读取hive数据报错:java.lang.RuntimeException: serious problem
问题: Caused by: java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: ...
- Python进阶----网络通信基础 ,OSI七层协议() ,UDP和TCP的区别 , TCP/IP协议(三次握手,四次挥手)
Python进阶----网络通信基础 ,OSI七层协议() ,UDP和TCP的区别 , TCP/IP协议(三次握手,四次挥手) 一丶CS/BS 架构 C/S: 客户端/服务器 定义: ...
- Matlab组合模式
组合模式(Composite),将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.组合模式的目的是让客户端不再区分操作的是组合对象(Compos ...
- python pip安装解决方法
一招解决python pip install 安装库失败 PIP是python强大的安装利器,但是我们经常遇到安装库失败的问题,以下本人觉得最有效的解决方法: 1.打开 https://www.l ...
- vue 数据更新问题
在uni-app构建选项卡时,方法中改变的数组数据 无法更新v-if中的布尔值 在函数中打印出来是修改成功了,但在页面中并没有进行响应 布局如下: <swiper :current=" ...