share my c++ solution with sort


  • 0
    class Solution {
    public:
        int findContentChildren(vector<int>& g, vector<int>& s) {
            sort(g.begin(),g.end());
            sort(s.begin(),s.end());
            int res = 0;
            for(int i=0;i<s.size()&&res<g.size();++i){
                if(s[i]>=g[res])
                    ++res;
            }
            return res;
        }
    };
    

    if child[res] can be content with cookie[i] ,
    then ++res,++i,
    else just ++i;
    finally , res is the number we want


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.