- 给定数组 people 。people[i]表示第 i 个人的体重 ,船的数量不限,每艘船可以承载的最大重量为 limit。
- 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit。
- 返回承载所有人所需的最小船数 。
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
// 因为最多可以承载2个人,所以要充分利用船的承载能力,使载两个人的船尽可能的多
int ans = 0;
sort(people.begin(), people.end());
int light = 0;
int heavy = people.size() - 1;
while(light <= heavy){
if(people[light] + people[heavy] > limit){
--heavy;
}
else{
++light;
--heavy;
}
++ans;
}
return ans;
}
};