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;
}
}

最新文章

  1. Linux C 字符串函数 sprintf()、snprintf() 详解
  2. 场景7 Data Guard
  3. SPF详解
  4. Android Studio Push rejected: Push to origin/Alpha1.0 was rejected
  5. 编译项目报错: Ignoring file / xxx , missing required architecture i386 in file / xxx (2 slices)
  6. UIButton 在 iOS7.0与iOS7.1 中关于enabled的一点区别
  7. Oracle入门学习笔记
  8. python2.7读汉字的时候出现乱码,如何解决
  9. 消息中间件ActiveMQ及Spring整合JMS的介绍
  10. [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
  11. 简单读!zookeeper单机模式的启动逻辑
  12. (详细)华为V9 DUK-AL20的usb调试模式在哪里打开的方法
  13. UVA1374-Power Calculus(迭代加深搜索)
  14. 云南-第一个应用节点-ssh登录-卡顿的问题
  15. 【IDEA&amp;&amp;Eclipse】3、IntelliJ IDEA 的 20 个代码自动完成的特性
  16. ajax评论
  17. Jupyter notebook 文件路径
  18. iOS UI-(多)视图控制器的生命周期、加载方法和模态视图方法以及屌丝方法
  19. Java开发环境的搭建(jdk,eclipse)
  20. mybatis if test 相等的情况怎样动态拼接sql

热门文章

  1. 6.Cookie和Session
  2. 《浏览器工作原理与实践》&lt;05&gt;渲染流程(上):HTML、CSS和JavaScript,是如何变成页面的?
  3. Cacti-0.8.8b详细安装及配置步骤
  4. 多线程模块的condition对象
  5. macOS 终端常用命令
  6. 如何让Python2与Python3共存
  7. springMVC的简单了解和环境搭建
  8. linux基础_用户组的管理
  9. [NOI2016]循环之美——结论+莫比乌斯反演
  10. unity 用代码控制动画的播放的进度