排序,然后看最重的能否跟最轻的一起走,如果可以,一起走,如果不行,单独走。
class Solution {
public int numRescueBoats(int[] people, int limit) {
int result = 0, left = 0, right = people.length - 1;
Arrays.sort(people);
while(left <= right){
if(left == right){
++result;
break;
}
if(people[left] + people[right] > limit){
++result;
--right;
}
else{
++result;
--right;
++left;
}
}
return result;
}
}