Simple Python Solution


  • 3
    class Solution(object):
        def findContentChildren(self, g, s):
            g.sort()
            s.sort()
            i, j = 0, 0
            while i < len(g) and j < len(s):
                if s[j] >= g[i]:
                    i += 1
                j += 1
            return i

  • 0
    S

    identical to mine, nice :)


  • 0

    @lilixu one flaw here, because you are an awesome parent, you should assign as big cookies as possible. So I think you should assign cookies in reverse order, though the result is the same. Below is my solution.

    class Solution(object):
        def findContentChildren(self, g, s):
            g.sort()
            s.sort()
            i,j = len(g)-1,len(s)-1
            count = 0
            while (i>=0 and j>=0):
                if g[i] <= s[j]:
                    count += 1
                    i-=1
                    j-=1
                else:
                    i -= 1
            return count
    

Log in to reply
 

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