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. python 删除特定字符所在行

    #查询文件中含有特殊字符串的行 #!/usr/bin/python # -*- coding:utf- -*- import re file1 = open('test.txt','r+') istx ...

  2. 了解jQuery的detach()和remove()

    jQuery中提供了两种移出一个DOM元素的方法detach()和remove(),虽然是一样的功能,但是给出两种方法,必然有它的不同之处. empty() 单独说一下 ,它删除当前元素的所有子元素, ...

  3. briup_JDBC

    连接mysql和orcle的驱动jar包 链接:https://pan.baidu.com/s/1M5RZY62UOZbfFGIwDQK6SQ  提取码:cu6e 复制这段内容后打开百度网盘手机App ...

  4. ie11浏览器不显示vbs脚本

    最初接触学习vbs在浏览器上运行,老不显示vbscript脚本语言,所以找了很久,最后就用这个方法吧,比较简单有效 原因:新版IE不再支持 VBScript,就是因为微软已经放弃把VBScript作为 ...

  5. 解决docker容器开启端口映射后,会自动在防火墙上打开端口的问题

    在docker中运行第三方服务时,通常需要绑定服务端口到本地主机.但使用 -p 参数进行的端口映射,会自动在iptables中建立规则,绕过firewalld,这对于端口级的黑白名单控制管理是很不利的 ...

  6. matlab FDA

    FDA是filter design analysis过滤器设计与分析的缩写.

  7. 010.简单查询、分组统计查询、多表连接查询(sql实例)

    -------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...

  8. [Abp vNext微服务实践] - 添加中文语言

    简介 abp vNext中提供了多语言功能,默认语言是英文,没有提供中文语言包.在业务开发中,定义权限后需要用中文的备注提供角色选择,本篇将介绍如何在abp vNext中加入中文语言. step1:添 ...

  9. JAVA遇见HTML——JSP篇(案例项目)

  10. ext系统的超级块

    什么是超级块 如果说inode块是Linux操作系统中文件的核心,那么超级块就是文件系统的心脏.启动Lnux操作系统后,发现某个文件系统无法使用,很有 可能就是超级块出现了问题.为什么这个超级块有这么 ...