Python straightforward solution - use stack.pop to assign cookies


  • 0
    P
    class Solution(object):
        def findContentChildren(self, g, s):
            """
            :type g: List[int]
            :type s: List[int]
            :rtype: int
            """
            g.sort()
            s.sort( reverse = True )
            count = 0
            for child in g:
            	while s and s[-1] < child:
            		s.pop()
            	if not s:
            		return count
            	s.pop()
            	count += 1
            return count
    

  • -1
    H

    Just Sort and Count

    public class Solution {
        public int findContentChildren(int[] g, int[] s) {
            Arrays.sort(g);Arrays.sort(s);int i=g.length-1;int j=s.length-1;int count=0;
                while(i>=0&&j>=0){
                if(g[i]<=s[j]){count++;i--;j--;}
                else{i--;}
                }
            return count;
        }
    }
    

Log in to reply
 

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