Tech for good

[Leetcode/Sort, Greedy, Two-pointer] 455. Assign Cookies 본문

IT/Computer Science

[Leetcode/Sort, Greedy, Two-pointer] 455. Assign Cookies

Diana Kang 2025. 5. 3. 03:32

Look through Example 

  • g = child A want 1 number of cookie, child B want 2 number of cookies, child C want 3 number of cookies
  • s = cookie A is 1 number of cookie, cookie B is 2 number of cookies
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()

        child = 0  # pointer to children
        cookie = 0  # pointer to cookies

        while child < len(g) and cookie < len(s):
            if s[cookie] >= g[child]:
                child += 1  # child is content, move to next child
            cookie += 1  # move to next cookie

        return child  # number of content children