Share my greedy solution


  • 0

    The idea is simple: greedy.

    public class Solution {
        public int findContentChildren(int[] g, int[] s) {
            if(s.length==0 || g.length==0) return 0;
            Arrays.sort(g);
            Arrays.sort(s);
            int child = 0, cookie = 0;
            while(child<g.length && cookie < s.length){
                if(g[child]<=s[cookie]) child++;
                cookie++;
            }
            return child;
        }
    }
    

Log in to reply
 

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