原题链接在这里: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. 有些CCS工程中为什么会有两个CMD文件?

    这里的CCS就是TI公司的Code Composer Studio 集成开发环境,所以这篇随笔也就是关于TI公司DSP开发过程中对于刚入门同学的一个常见问题了. 那作为一个初学者,对于CCS工程中的c ...

  2. Array : 数组的常用方法

    数组常用方法 对象通用方法 1.toLocalString()  数组中的每个元素都会调用toLocalString(),然后以逗号隔开, 拼接为字符串: 2.toString() 数组中的每个元素都 ...

  3. golang ---常用函数:make

    简介 内建函数 make 用来为 slice,map 或 chan 类型分配内存和初始化一个对象(注意:只能用在这三种类型上) slice // 长度为5,容量为10的slice,slice中的元素是 ...

  4. Maven的仓库和settings.xml配置文件

    (尊重劳动成果,转载请注明出处:https://blog.csdn.net/qq_25827845/article/details/83549846冷血之心的博客) 快速导航: Maven基础概念和安 ...

  5. Linux学习笔记之tail命令显示最后n行

    tail :输出文件的最后几行. 用于linux查看日志的时候很方便,假如日志文件为:Console.log用法:1. tail Console.log tail # 输出文件最后10行的内容 2. ...

  6. Docker 四种网络模式

    原文 https://www.cnblogs.com/gispathfinder/p/5871043.html 我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络 ...

  7. 浮动IP地址(Float IP)与 ARP欺骗技术

    浮动IP地址: 一个网卡是可以添加多个IP的. 就是多个主机工作在 同一个集群中,即两台主机以上.每台机器除了自己的实IP外,会设置一个浮动IP,浮动IP与主机的服务(HTTP服务/邮箱服务)绑在一起 ...

  8. Spring Security Architecture and Implementation(架构和实现)学习笔记

    Spring Security 关于spring-security的官网文档学习笔记,主要是第8章 Architecture and Implementation(架构和实现)内容 参考: https ...

  9. 文件的内存读取 ,以及image图片(二进制)的读取

    #在python2.x中导入模块方法: from StringIO import String #在python2.x中它还有个孪生兄弟,运行速度比它快,用c实现的 from cStringIO im ...

  10. MySQL Install--MySQL安装相关知识和问题

    1.mysqld和mysqld_safe之间关系 mysqld_safe是一个启动脚本,用来启动MySQL服务器并监控MySQL服务的运行情况,并在其服务出现故障宕机后重启其服务. mysql.ser ...