LeetCode 双指针类题解

2019-06-18  本文已影响0人  DejavuMoments

881. Boats to Save People

Note:
1 <= people.length <= 50000
1 <= people[i] <= limit <= 30000

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        
        if(people.empty()) return 0;
        std::sort(people.begin(), people.end());
        
        int i = 0; 
        int j = people.size() - 1;
        int ans = 0;
        
        while(i <= j){
            ans++;
            if(people[i] + people[j] <= limit)
                i++;
            j--;
        }
        
        return ans;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读