3-line C++ greedy solution: doesn't sound like an awesome parent, don't you think? :-)


  • 0

    Always give the smallest cookie possible to the least greed child, which doesn't sound like an awesome parent as stated in the problem but fair, don't you think? :-)

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

Log in to reply
 

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