881. Boats to Save People

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end());
        int res = 0;
        for (int i = 0, j = people.size()-1; i <= j; ) {
            if (people[i] + people[j] <= limit) {
                i++;
                j--;
            }
            else
                j--;
            res++;
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/JTechRoad/p/9998904.html
今日推荐