[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]...* ...
随机推荐
- echarts画中国地图并上色
任务是画一个中国地图,并在指定区域上颜色,学姐说用arcgis画,乖乖,4个g的安装文件,算了, 还是echarts大法好..如果想熟悉这个牛X的工具,请移步https://www.w3cschool ...
- English Voice of << Last Christmas >>
Last Christmas填 词:乔治·迈克尔谱 曲:乔治·迈克尔编 曲:乔治·迈克尔歌词:Last Christmas I gave you my heart去年的圣诞节,我把心给了你But th ...
- OnSen UI结合AngularJs打造”美团"APP"订单”页面 --Hybrid App
1.页面效果图: 演示链接地址:http://www.nxl123.cn/bokeyuan/meiTuanDemo_order/ 2.核心代码 order.html: <ons-page id= ...
- LeetCode--367--有效的完全平方数
问题描述: 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: 输入:16 输 ...
- 第二阶段——个人工作总结DAY01
今天开始第二阶段的冲刺. 今天准备做什么:首先要先学习活动之间通过TextView用Intent实现跳转. 遇到的困难:无.
- stl中常用的排序算法
#include"iostream" #include"vector" using namespace std; #include"string&qu ...
- HTML 5 <span> 标签
标签定义及使用说明 <span> 用于对文档中的行内元素进行组合. <span> 标签没有固定的格式表现.当对它应用样式时,它才会产生视觉上的变化.如果不对 <span& ...
- EF - Database First 开发方式
概述 Database First 又叫数据库优先的开发方式,是一种比较旧的开发方式,现在越来越多的企业已经不再使用此种开发方式. 当然,对于一些旧项目进行升级,在已经有了数据库的情况下,使用此种方式 ...
- C# 3.0 / C# 3.5 系统内置委托
内置委托的定义声明: System.Func,代表有返回类型的委托: public delegate TResult Func<out TResult>(); public delegat ...
- DBMS_ROWID定位数据行物理存储位置
对于Oracle中的堆表,我们可以通过oracle内置的ROWID伪列得到对应行记录所在的ROWID的值(注意,这个ROWID只是一个伪列,实际的块中并不存在该列).然后我们可以通过DBMS_ROWI ...