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 <= 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的更多相关文章
- 【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)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
随机推荐
- ubuntu linux常用指令(1)
序号 命令 说明 1 sudo su 从普通用户切换到root用户 2 su user 从root用户切换到普通用户 3 ls 列出当前目录的文件和目录,但是不包括隐藏文件和目录 4 ls -a 列出 ...
- Sso单点登录分析
1. Sso系统分析 1.1. 什么是sso系统 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这 ...
- go_变量定义
package main import "fmt" var( aa =3 bb ="kkk" cc =true )//go语言中,变量可以定义在函数外面,并不是 ...
- mybatis+oracle如何批量执行多条update
接口 public void setStatus(List<Columns> columnsList); mapping xmlmapping 中使用foreach,关于标签的使用,资料非 ...
- SEL 类型
1.SEL类型的第一个作用, 配合对象/类来检查对象/类中有没有实现某一个方法 SEL sel = @selector(setAge:); Person *p = [Person new]; // 判 ...
- 富文本编辑器和fastdfs的使用
宜立方商城的系统架构a) 功能介绍(项目架构,有哪些功能模块,这些功能模块如何实现?)b) 架构讲解工程搭建-后台工程c) 使用maven搭建工程(后台工程如何搭建?)d) 使用maven的tomca ...
- C++ std::vector<bool>
std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...
- loadrunner-27077报错解决办法
警告 -27077: “每次迭代模拟一个新用户”运行时设置为“开”时,“vuser_init”节将包含 Web 函数.这可能会产生具有多次迭代的不可预测结果 [MsgId: MWAR-27077] ...
- C++细节理解
1.为什么static类外初始化不需要static关键字 答:因为类外static变量或函数表示限定在此源文件中才能使用,而类中的static变量或函数表示由本类及其所有对象共享,如果在类外初始化或定 ...
- Microsoft(C)注册服务器(32位)CPU占用高
Microsoft(C)注册服务器(32位)CPU占用高 摘自:https://blog.csdn.net/jtsqrj/article/details/83034252 2018年10月12日 23 ...