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