美文网首页
LeetCode 双指针类题解

LeetCode 双指针类题解

作者: DejavuMoments | 来源:发表于2019-06-18 00:09 被阅读0次

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;
    }
};

相关文章

网友评论

      本文标题:LeetCode 双指针类题解

      本文链接:https://www.haomeiwen.com/subject/udhmfctx.html