Assign Cookies - Problem

You're a parent who wants to make your children happy by giving them cookies! ๐Ÿช But there's a catch - you can only give each child at most one cookie.

Here's the situation:

  • Each child i has a greed factor g[i] - the minimum size cookie they'll be satisfied with
  • Each cookie j has a size s[j]
  • A child i will be content if they receive a cookie where s[j] >= g[i]

Your goal: Maximize the number of content children by optimally assigning cookies.

Example: If you have children with greed factors [1, 2, 3] and cookies with sizes [1, 1], you can only satisfy 2 children (those with greed factors 1 and 2) by giving them the cookies of size 1 each.

Input & Output

example_1.py โ€” Basic Case
$ Input: g = [1,2,3], s = [1,1]
โ€บ Output: 1
๐Ÿ’ก Note: You have 3 children with greed factors 1, 2, 3 and 2 cookies of size 1. Only one child (with greed factor 1) can be satisfied.
example_2.py โ€” Perfect Match
$ Input: g = [1,2], s = [1,2,3]
โ€บ Output: 2
๐Ÿ’ก Note: Both children can be satisfied. Child with greed 1 gets cookie of size 1, child with greed 2 gets cookie of size 2.
example_3.py โ€” No Satisfaction Possible
$ Input: g = [5,6], s = [1,2,3]
โ€บ Output: 0
๐Ÿ’ก Note: All children have greed factors larger than any available cookie size. No child can be satisfied.

Visualization

Tap to expand
๐Ÿช Smart Cookie Distribution StrategyChildren (by greed)๐Ÿ˜Š 1๐Ÿ˜ 2๐Ÿ˜ค 7๐Ÿ˜ก 9Least picky โ†’ Most pickyCookies (by size)๐Ÿช1๐Ÿช2๐Ÿช3๐Ÿช8Smallest โ†’ Largestโœ“ Greedy AssignmentSmallest suitable cookieto least greedy child๐ŸŽฏ Result: 3 out of 4 children satisfied!Child(1)โ†’Cookie(1), Child(2)โ†’Cookie(2), Child(7)โ†’Cookie(8)Child(9) cannot be satisfied with remaining cookies
Understanding the Visualization
1
Line up children by greed
Arrange children from least picky to most picky
2
Sort cookies by size
Arrange cookies from smallest to largest
3
Use greedy strategy
Give the smallest acceptable cookie to each child
4
Maximize happiness
This approach ensures maximum number of satisfied children
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because satisfying the least demanding child first with the smallest possible cookie preserves larger cookies for more demanding children later.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^(m+n))

For each child-cookie pair, we have multiple choices leading to exponential combinations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Recursion depth can go up to n children

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค g.length โ‰ค 3 ร— 104
  • 0 โ‰ค s.length โ‰ค 3 ร— 104
  • 1 โ‰ค g[i] โ‰ค 231 - 1
  • 1 โ‰ค s[j] โ‰ค 231 - 1
  • Each child can receive at most one cookie
Asked in
Amazon 45 Google 38 Facebook 32 Apple 28
78.5K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen