import java.util.Arrays;
class Solution {
public int numRescueBoats(int[] people, int limit) {
int len = people.length;
int ans = 0;
Arrays.sort(people);
int l = 0;
int r = len - 1;
while (l <= r) {
if (people[l] + people[r] <= limit) {
l++;
}
r--;
ans++;
}
return ans;
}
}
Leetcode_881_救生艇_双指针
最新推荐文章于 2025-07-28 18:51:26 发布
本文介绍了一种名为Solution的类,它使用排序和两指针技巧解决了一个问题:在限制船只载重量的情况下,如何用最少的船只救出所有人员。通过将人群按大小排序并动态调整船头和船尾的人选,确保每艘船的负载不超过限制,从而计算所需的最小船只数量。

1533

被折叠的 条评论
为什么被折叠?



