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

这道题让我们载人过河,说是每个人的体重不同,每条船承重有个限度 limit(限定了这个载重大于等于最重人的体重),同时要求每条船不能超过两人,这尼玛是独木舟吧,也就比 kayak 大一点点吧(不过也有可能是公园湖中的双人脚蹬船,怀念小时候在公园划船的日子~),问我们将所有人载到对岸最少需要多少条船。从题目中的例子2可以看出,最肥的人有可能一人占一条船,当然如果船的载量够大的话,可能还能挤上一个瘦子,那么最瘦的人是最可能挤上去的,所以策略就是胖子加瘦子的上船组合。那么这就是典型的贪婪算法的适用场景啊,首先要给所有人按体重排个序,从瘦子到胖子,这样我们才能快速的知道当前最重和最轻的人。然后使用双指针,left 指向最瘦的人,right 指向最胖的人,当 left 小于等于 right 的时候,进行 while 循环。在循环中,胖子是一定要上船的,所以 right 自减1是肯定有的,但是还是要看能否再带上一个瘦子,能的话 left 自增1。然后结果 res 一定要自增1,因为每次都要用一条船,参见代码如下:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/881

参考资料:

https://leetcode.com/problems/boats-to-save-people/

https://leetcode.com/problems/boats-to-save-people/discuss/156740/C%2B%2BJavaPython-Two-Pointers

https://leetcode.com/problems/boats-to-save-people/discuss/156855/6-lines-Java-O(nlogn)-code-sorting-%2B-greedy-with-greedy-algorithm-proof.

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

最新文章

  1. 解决CURL 请求本地超时
  2. oracle sys as dba
  3. java常用开发工具类之 图片水印,文字水印,缩放,补白工具类
  4. having与where区别
  5. 如何进去bios设置
  6. Windows 7 64位下解决不能创建Django项目问题
  7. GitHub托管BootStrap资源汇总
  8. 请教下关于CKEditor富文本编辑框设置字体颜色的问题
  9. OSGI框架中通过BundleContext对象对服务的注册与引用
  10. 理解及操作环境变量(基于Mac操作)
  11. 《java.util.concurrent 包源码阅读》08 CopyOnWriteArrayList和CopyOnWriteArraySet
  12. Numpy入门 - 数组排序
  13. hibernate--CRUD初体验
  14. Windows -- cmd命令: ipconfig 和 nbtstat
  15. 为什么ArrayList、LinkedList线程不安全,Vector线程安全
  16. c# mvc 在控制器中动态解析cshtml文件并获取对应的html代码
  17. 关于python的一些想法
  18. pip国内镜像
  19. SQLite 知识摘要 --- 事务
  20. 关于bjam编译自己模块出错的问题

热门文章

  1. JeeSite | 数据权限应用
  2. RESTful及API设计(原)
  3. 永久清理git中的历史大文件
  4. Python urllib与requests、XML和HTMLParser
  5. Java学习——单元测试JUnit
  6. 查看java程序的指令码
  7. IO流总结---- 字节流 ,字符流, 序列化 ,数据操作流,打印流 , Properties 集合
  8. 使用 docker-compose 运行 MySQL
  9. RV32I基础整数指令集
  10. ptrace函数深入分析