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

Approach #1: C++.

class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
int ans = 0;
sort(people.begin(), people.end());
int i = 0, j = people.size()-1;
while (i <= j) {
ans++;
if (people[i] + people[j] <= limit)
i++;
j--;
}
return ans;
}
};

  

Analysis:

Because a boat can carry two people. if the boat can carry the max weight people and the min weight people, then do so. Otherwise, the boat carry the max weight people only.

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

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

  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. jquery 报错 Uncaught TypeError: Illegal invocation

    遇到这个错误 请检查你的ajax提交方法的参数 1 参数是否都有定义 2 参数个数是否一致 3参数是否都有值(******)

  2. nginx 多域名配置,采用多配置文件的方式

    nginx 中多域名配置,目前采用多配置文件的方式. 配置过程比较简单. 首先在 nginx 目录下创建子目录 vhosts . 在 vhosts 目录中创建对应域名的配置文件.如有域名 898hi. ...

  3. 【HDU4405】Aeroplane_chess

    题意 飞行棋.有n+1格,开始时在0号格子,每一步都要扔一个dice(六个面,概率相同)哪一面朝上他就会向前走x+i步.当x+i大于等于N的时候,游戏结束.另外,地图上有m条航线.第i条航线可以直接从 ...

  4. UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释(转)

    layoutSubviews总结 ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)sizeToFit—————— - (void ...

  5. HEIDSOFT

    HEIDSOFT ENTHUSIASTIC GITHUB USER heidsoft@sina.com GitHub Profile I'm a developer based in China.sh ...

  6. CentOS 6.4一键自动化安装ISO镜像光盘

    下载CentOS-6.4-x86_64-minimal.iso 1 http://mirrors.163.com/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-mi ...

  7. php static 变量声明

    <?phpfunction test($key){ static $array = array();  /* 静态变量是只存在于函数作用域中的变量,注释:执行后这种变量不会丢失(下次调用这个函数 ...

  8. 技术串讲 CAS 有用

    CAS,全称为Compare and Swap,即比较-替换.假设有三个操作数:内存值V.旧的预期值A.要修改的值B,当且仅当预期值A和内存值V相同时,才会将内存值修改为B并返回true,否则什么都不 ...

  9. ubuntu如何安装samba

    1.samba安装sudo apt-get install samba2.修改smb.confsudo gedit /etc/samba/smb.conf 文件最后增加如下代码:[share] pat ...

  10. Flask框架 之 信号

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. 安装 pip3 install blinker 内置信号 request_started = ...