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 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 <= 500001 <= 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的更多相关文章
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 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 ...
- [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 ...
- LeetCode 881. Boats to Save People
原题链接在这里:https://leetcode.com/problems/boats-to-save-people/ 题目: The i-th person has weight people[i] ...
- [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 ...
- [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 ...
- 【leetcode】885. Boats to Save People
题目如下: 解题思路:本题可以采用贪心算法,因为每条船最多只能坐两人,所以在选定其中一人的情况下,再选择第二个人使得两人的体重最接近limit.考虑到人的总数最大是50000,而每个人的体重最大是30 ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
随机推荐
- excel解析的几种实现方法
- 高性能Web服务器Nginx的配置与部署研究(16)小议location匹配模式优先级
1 location 的匹配符 1.1 等于匹配符:= 等于匹配符就是等号,特点可以概括为两点: 精确匹配 不支持正则表达式 1.2 空匹配符 空匹配符的特点是: 匹配以指定模式开始的 URI 不支持 ...
- 新做的系统,第一次拉maven项目时,鼠标左键+ctrl键不能进方法
对项目选择属性,跳转至:选择以下步骤
- HTML5新增的非主体结构元素
-------------------siwuxie095 HTML5 新增的非主体结构元素 1.header 元素 ...
- Python open()文件处理使用介绍
1. open()语法open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])open函数有很多 ...
- 跨版本mysqldump恢复报错Errno1449
已经有一套主从mysql,新增两个slave主库Server version: 5.6.22-log MySQL Community Server (GPL)旧从库Server version: 5. ...
- OCFS2 Fencing
OCFS2 FencingPosted on February 8, 2011 by Abdulhameed Basha I am very excited to start writing my e ...
- [C++] const object
const object const 对象只能调用const函数 const函数不能改变一般成员变量的值,但是mutable的变量不受限制
- 如何优雅地使用命令行设置windows文件关联
如何优雅地使用命令行设置windows文件关联 使用ftype查看帮助 设置关联所需命令有ftype assoc,需要管理员权限.如果忘记使用方法可通过ftype的帮助获取查看方法 C:\WINDOW ...
- 实践作业4:Web测试实践(小组作业)每日任务记录3
会议时间:2017年12月23日 会议地点:东九教学楼自习区 主 持 人:王晨懿 参会人员:王晨懿.余晨晨.郑锦波.杨潇.侯欢.汪元 记 录 人:王晨懿 会议议题:小组作业第二阶段 下面是今天 ...