原题链接在这里: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. jquery下拉单选框可左右移动数据

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. C++静态库与动态库的区别

    在日常开发中,其实大部分时间我们都会和第三方库或系统库打交道.在 Android 开发音视频开发领域,一般会用到 FFmepg.OpenCV.OpenGL 等等开源库, 我们一般都会编译成动态库共我们 ...

  3. BZOJ5312 冒险 势能分析、线段树

    传送门 区间位赋值.区间求最大值似乎是不能够像一般的线段树一样直接打标记的,但是直接暴力也太没有面子了. 我们考虑优化一下暴力:如果说线段树的一段区间内在当前修改的所有位置上所有数都是相同的,那么这个 ...

  4. 《JAVA高并发编程详解》-wait和sleep

  5. linux服务器同时运行两个或多个tomcat

    第一步:条件:配置好jdk,如果没有配置好请看:https://www.cnblogs.com/weibanggang/p/11460537.html 第二步:下载安装tomcat 下载tomcat到 ...

  6. 高性能Golang研讨会【精】

    by Dave Cheney 概观 本次研讨会的目标是为您提供诊断Go应用程序中的性能问题并进行修复所需的工具. 通过这一天,我们将从小工作 - 学习如何编写基准,然后分析一小段代码.然后走出去讨论执 ...

  7. python预习day1

    计算机基础 cpu 大脑 内存 临时记忆 硬盘 永久记忆 输入设备 眼睛 耳朵 输出设备 嘴巴 操作系统 控制计算机硬件工作流程的 应用程序 安装在操作系统之上的软件 python简介 python是 ...

  8. MySQL数据库汇总

    -- mysql的最大连接数:默认为 100   -- mysql的增删改查   -- mysql统计各个字段(case when 用法 注:也可以使用其他的)   select (case when ...

  9. 【面试突击】-RabbitMQ常见面试题(二)

    问题一:RabbitMQ 中的 broker 是指什么?cluster 又是指什么? 答:broker 是指一个或多个 erlang node 的逻辑分组,且 node 上运行着 RabbitMQ 应 ...

  10. 类选择器与ID选择器的比较

    如果已经在元素中标识了class或id,就可以在选择器中使用这个标准,从而只对已标识的元素进行格式化.不过推荐使用类选择器,一会儿我会解释理由. 要在class选择器和id选择器之间作出选择的时候,建 ...