Simple understand Java Code (consider the comparison from right side)


  • 0
    N
    public class Solution {
        public int findContentChildren(int[] g, int[] s) {
            Arrays.sort(g);
            Arrays.sort(s);
            
            int gl = g.length-1; //g last index 
            int sl = s.length-1; //s last index
            int res = 0;
            while(gl>=0 && sl>=0){
                if(s[sl]>=g[gl]){
                    res++;
                    sl--;
                    gl--;
                }
                else{
                    //if s[sl]<g[gl]....g[gl] cannot be content
                    gl--;
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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