Easy to understand solution


  • 0
    K

    Basically, you want to sort both arrays and start from the smallest size. Start with the first child (smallest) and try to find a suitable cookie. Once a good cookie found, increment the count and move to the next child.

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

Log in to reply
 

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