↓Skip to main content
  1. LeetCode/

LeetCode 455: Assign Cookies

·2 mins· ·
LeetCode Easy Array Two-Pointers Greedy Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-24

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer → Runtime Error → Wrong Answer → Runtime Error → Wrong Answer → Runtime Error → Wrong Answer → Accepted

Approach
#

Greedy: the least greedy child is easiest to satisfy, so give them the smallest cookie that works and save bigger ones (Two Pointers Template 2). After sorting both, look at cookies one by one: if it satisfies the current child, give it (advance the child pointer); otherwise try the next, larger cookie.

My Attempts
#

This took 10 submissions in 2022:

  • Attempt 1 zipped the two sorted arrays and compared them one-to-one, so a cookie too small left that child with nothing: appetites [5] and cookies [5, 4] paired 4 with 5, returning 0 instead of 1.
  • Loop versions then kept reading g[0] after every child was fed, or counted 1 with an empty cookie list, going out of range or overcounting.

Solution
#

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        i = 0
        for size in s:
            if i < len(g) and size >= g[i]:
                i += 1
        return i

Complexity
#

Time O(m log m + n log n), space O(1) besides sorting.

Mistakes to Avoid
#

  • Do not zip the sorted arrays one-to-one: skip a cookie that is too small rather than giving up on that child.
  • Stop once every child is satisfied instead of reading g[i] further.

Interview Explanation
#

I sort appetites and cookies and scan cookies from smallest: if it satisfies the least greedy remaining child, that child is done, otherwise the next larger cookie is tried. Each pointer moves once; sorting dominates at O(n log n).

Related

LeetCode 414: Third Maximum Number
·1 min
LeetCode Easy Array Sorting
LeetCode 414 note: track the three largest distinct values, skipping repeats, and return the maximum if fewer than three distinct values exist
LeetCode 283: Move Zeroes
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 283 note: the write pointer advances only on non-zero values, swapping with them, which pushes zeros to the back
LeetCode 217: Contains Duplicate
·1 min
LeetCode Easy Array Hash-Table Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate
LeetCode 169: Majority Element
·2 mins
LeetCode Easy Array Hash-Table Divide-and-Conquer Sorting
LeetCode 169 note: Boyer-Moore voting, where different values cancel in pairs and the survivor is the one appearing more than half the time
LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written
LeetCode 26: Remove Duplicates from Sorted Array
·2 mins
LeetCode Easy Array Two-Pointers
LeetCode 26 note: overwrite in place with a write pointer for the next slot, writing only values different from the last one written