原题链接在这里: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的更多相关文章

  1. [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 ...

  2. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 【leetcode】885. Boats to Save People

    题目如下: 解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近limit.考虑到人的总数最大是50000,而每个人的体重最大是30 ...

  7. [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 ...

  8. LeetCode 881.救生艇(C++)

    第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. STM8L052C6T6液晶LCD驱动过程

    首先还是必要的说明,鄙人经验有限,如发现问题还请不吝赐教,反馈到邮箱mr.li.ming@qq.com 使用的液晶屏 单片机引脚 液晶的COM1-COM4分别对应单片机LCD_COM0-LCD_COM ...

  2. Vuecli3

    第一步安装 npm install -g @vue/cli 第二步关于项目配置 因为cli3去除了cli2中index.html 转而存到了publi文件中 如果需要配置跨域 页面入口 打包文件路径都 ...

  3. httpd服务的配置及应用

    一.httpd服务的配置文件 httpd服务的主配置文件通常为httpd根目录下的conf/httpd.conf文件,通过yum安装的httpd服务的主配置路径通常如下: httpd-2.2:/etc ...

  4. c# js 时间

    DateTime GetTime(double timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new Dat ...

  5. ansible 中 JAVA_HOME不生效问题

    解决方案 ~/.bash_profile 是交互式.login 方式进入 bash 运行的,意思是只有用户登录时才会生效. ~/.bashrc 是交互式 non-login 方式进入 bash 运行的 ...

  6. laravel项目中通过nvmw安装node.js和npm 开发环境-- windows版

    windows版本安装 此教程执行的时候,网速一定要好.不然可能出现各种错误. 如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ git clone nvmw  直接从 githu ...

  7. CI框架扩展系统类库

    CI框架不支持像yii2框架那样,可以直接在controllers下创建CommonController并继承父类,那么我们想要做登录控制或权限控制时,直接在父类控制器操作是不合理的. 这时比较方便的 ...

  8. [转载]Mysql数据库千万级数据处理优化

    转载:http://blog.sina.com.cn/s/blog_6dcd17320100tm6o.html 1. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by ...

  9. CVE-2019-0193:Apache Solr 远程命令执行漏洞复现

    0x00 漏洞背景 2019年8月1日,Apache Solr官方发布了CVE-2019-0193漏洞预警,漏洞危害评级为严重 0x01 影响范围 Apache Solr < 8.2.0 0x0 ...

  10. HDU4548美素数——筛选法与空间换时间

    对于数论的学习比较的碎片化,所以开了一篇随笔来记录一下学习中遇到的一些坑,主要通过题目来讲解 本题围绕:素数筛选法与空间换时间 HDU4548美素数 题目描述 小明对数的研究比较热爱,一谈到数,脑子里 ...