解题思路
贪心
代码
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int ans = 0;//救生艇数量
int nums = people.length;
int left = 0,right = nums-1;
while (nums!=0 && left<=right){
int capacity = limit;
capacity = capacity - people[right];
right--;
nums--;
if (people[left]<=capacity){
left++;
nums--;
}
ans++;
}
return ans;
}
}