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

Runtime: 44 ms, faster than 38.51% of Java online submissions for Boats to Save People.

class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int ret = ;
int right = people.length-;
int left = ;
while(right > left){
if(people[right] + people[left] <= limit){
ret++;
right--;
left++;
}else {
ret++;
right--;
}
}
if(right == left) ret++;
return ret;
}
}

LC 881. Boats to Save People的更多相关文章

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

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

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

  3. 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. LeetCode 881. Boats to Save People

    原题链接在这里:https://leetcode.com/problems/boats-to-save-people/ 题目: The i-th person has weight people[i] ...

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

  7. 【leetcode】885. Boats to Save People

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

  8. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  9. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

随机推荐

  1. mysql详解常用命令操作,利用SQL语句创建数据表—增删改查

    关系型数据库的核心内容是 关系 即 二维表 MYSQL的启动和连接show variables; [所有的变量] 1服务端启动 查看服务状态 sudo /etc/init.d/mysql status ...

  2. 【2】Kafka概念及原理

    1.Kafka背景 1.1.Kafka概要  Apache Kafka是一个开源的.轻量级的.分布式的.可分区的.可复制备份的.基于zookeeper协调管理的分布式流式消息系统.由Scala写成,支 ...

  3. Mysql(三):表操作

    一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 详见:http://www.cnblogs.com/6324TV/p/8481061.html 二 表介绍 表相当于文 ...

  4. java学习笔记12-继承

    继承就是子类继承父类的特征和行为 有时候单一划分某个类别并不能处理所有情况,某些类别下有明显不同的子类,这些子类虽然拥有类似的行为和属性,但是他们各自发生的这些行为的方式或者属性对某些结果的影响是不一 ...

  5. 浅谈IT人的发展(转载)

    一个人如果能确定他喜欢的行业,他一生都会非常幸福. 相反,则往往痛苦,也许竟然会因此成为一个哲学家也说不定. 中国的贫穷决定了我们当中的大多数人不能根据自己的爱好来选择职业,而只是因为生活所迫,或者世 ...

  6. mysql主从同步监控---邮件告警

    #!/bin/bash #check MySQL_Slave Status #crontab time : MYSQLPORT=`netstat -na|grep "|awk -F[:&qu ...

  7. SpringMVC问题整理

    JSP页面无法获取ModelAndView里的值 自己搭的项目突然EL表达式取不到值了 不管是用 ${msg} 还是用JSTL的<c:out value="${msg}"/& ...

  8. Mapreduce案例之Pi值估算

    题目: 这个程序的原理是这样的.假如有一个边长为1的正方形.以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形.在正方形里随机生成若干的点,则有些点是在扇形内,有些点是 ...

  9. flutter flutter_cupertino_date_picker 时间插件的用法

    https://blog.csdn.net/sinat_37255207/article/details/100041023 https://github.com/wuzhendev/flutter- ...

  10. C# IP地址和DNS 网络(三)

    Uri i = new Uri("http://www.baidu.com"); //可获取属性 UriBuilder u = new UriBuilder("http: ...