Simplest solution


  • 0
    B
    public class Solution {
        public int findContentChildren(int[] g, int[] s) {
            //sort the children and cookies
            Arrays.sort(g);
            Arrays.sort(s);
            int childIndex=0;
            int cookieIndex=0;
            while(childIndex<g.length && cookieIndex<s.length){
                //if cookie big enough for child, move to next child
                if(g[childIndex]<=s[cookieIndex]){
                    childIndex++;
                }
                //move to next cookie
                cookieIndex++;
            }
            return childIndex;
        }
    }

Log in to reply
 

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