[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.)
船只能坐两人,有体重限制,找出最少船只数。
【思路】
贪心算法,sort排序数组,头尾指针p、q。
每次取最重的,如果未超过limit,【顺走】最轻的。
所以每次最重的尾指针q和答案ans必变化,轻的头指针p只在没超重时,即limit条件下减少。
【代码】
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int p=0,q=people.length-1,ans=0;
while(p<=q){
ans++;
if(people[p]+people[q]<=limit){
p++;
}
q--;
}
return ans;
}
}
【例子】
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)
[Leetcode 881]船救人 Boats to Save People 贪心的更多相关文章
- 【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 ...
- [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
原题链接在这里:https://leetcode.com/problems/boats-to-save-people/ 题目: The i-th person has weight people[i] ...
- 【leetcode】885. Boats to Save People
题目如下: 解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近limit.考虑到人的总数最大是50000,而每个人的体重最大是30 ...
- 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 ...
- [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. 返回载到每一个人所需的最小船数.(保证每个人都 ...
- [LeetCode]面试题14- I. 剪绳子(DP/贪心)
题目 给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m.n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]...k[m] .请问 k[0]k[1]...* ...
随机推荐
- css趣味案例:画三角形
代码: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&q ...
- RHEL 5 , 用安装CD作为YUM的Repository
官方文档写的非常好 14.5. Upgrading the System Off-line with ISO and Yum Create a target directory to mount yo ...
- S-Nim HDU - 1536
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using ...
- 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...
- HashMap的两种排序方式
Map<String, Integer> map = new HashMap<String, Integer>();map.put("d", 2);map. ...
- 牛客寒假算法基础集训营6 J-迷宫
题目请点这里 分析:这是一道BFS的模板题,构造一个队列,将每个满足条件的(不超过边界,不超过左右移动次数的限制)位置推入队列,如果不是障碍物且没到达过,就将可到达位置的个数加1 此外,注意这里的输入 ...
- Spring AOP实现Mysql数据库主从切换(一主多从)
设置数据库主从切换的原因:数据库中经常发生的是“读多写少”,这样读操作对数据库压力比较大,通过采用数据库集群方案, 一个数据库是主库,负责写:其他为从库,负责读,从而实现读写分离增大数据库的容错率. ...
- <Matlab-3:追赶法(Doolittle分解)工具箱
function x=chase (d,e,f,b) % --------------------------------------------------------------- %the me ...
- 使用navigator.userAgent.toLowerCase()判断移动端类型
使用navigator.userAgent.toLowerCase()判断移动端类型 判断设备,区分Android,iphone,ipad和其它 var ua = navigator.userAgen ...
- 用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...