Python concise & efficient solution


  • 3

    My solution from the contest:

    def findContentChildren(self, g, s):
        g.sort()
        s.sort()
        res = 0
        i = 0
        for e in s:
            if i == len(g):
                break
            if e >= g[i]:
                res += 1
                i += 1
        return res
    

    O(nlogn) time and O(1) space


  • 0
    W
    This post is deleted!

  • 2
    W

    @dalwise excuse me.why use "res" and "i"?
    is i==res?


  • 0

    It is not O(NlogN). It's O(NlogN+MlogM)

    def findContentChildren(self, g, s):
            i, j, g, s = 0, 0, sorted(g), sorted(s)
            while i < len(g) and j < len(s):
                i += g[i] <= s[j]
                j += 1
            return i

  • 0
    G

    @dalwise yes,your solution is great,and my thoughts is same,this is my code:

    class Solution(object):
        def findContentChildren(self, g, s):
            g=sorted(g);s=sorted(s)
            count=0;i=0
            while count<len(g) and i<len(s):
                if g[count]<=s[i]:count+=1
                i+=1
            return count
                
    

Log in to reply
 

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