leetcode455

因为每个孩子最多一块饼干就比较简单,能满足一个孩子就发,不需要考虑多个饼干的叠加,如饼干1,2,3 孩子2,4,此时下面算法是错的

int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int i = 0, j=0;
        while(i<g.size() && j<s.size()){  g是孩子,s是饼干
            if(s[j]>=g[i])
                i++; // when the child get the cookie, foward child by 1
            j++;
        }
        return i;
    }

转自
https://leetcode.com/problems/assign-cookies/discuss/93991/Easy-Understanding-C%2B%2B-solution-O(nlogn)

你可能感兴趣的:(leetcode)