Skip to content

Latest commit

 

History

History

assign-cookies

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
title author type date url desc source
LeetCode - assign-cookies challenge solution
tzookb
post
2020-07-07 09:07:08 +0000
/leetcode/assign-cookies
leetcode assign-cookies exercise

This is based on the "greedy algorithm". But its relativelly easy todo. First we sort the cookies and children. I did in a descending order. But I think its not relevant here, as we are trying to find the amount of happy children and not fit exact numbers.

So after we sorted out we just iterate until we are out of children or cookies. We will change the cookie index each time we find a match with c child.

In the end the amount happy children with be the cookie index, as it incremented each time we found a cookie-child match.

def findContentChildren(g: List[int], s: List[int]) -> int:
    g.sort(reverse=True)
    s.sort(reverse=True)
    cookieIdx = childIdx = 0
    while childIdx < len(g) and cookieIdx < len(s):
        if g[childIdx] <= s[cookieIdx]:
            cookieIdx += 1
        childIdx += 1
    return cookieIdx