天天看点

881. Boats to Save People(救生船)———附带思路和完整代码0 效果1 题目2 思路3 代码

0 效果

1 题目

2 思路

3 代码

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end());
        int ans = 0, l = 0, r= people.size() - 1;
        while (l <= r){
            if(l == r){
                ans++;
                break;
            }else if(people[l] + people[r] > limit){
                r--;
                ans++;
            }else{
                r--;
                l++;
                ans++;
            }
        }
        return ans;
    }
};
           

继续阅读