Assign Cookies - Problem
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j].
If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
Input & Output
Example 1 — Basic Case
$
Input:
g = [1,2,3], s = [1,1]
›
Output:
1
💡 Note:
Child with greed factor 1 can be satisfied with cookie of size 1. Children with greed factors 2 and 3 cannot be satisfied with remaining cookies.
Example 2 — Perfect Match
$
Input:
g = [1,2], s = [1,2,3]
›
Output:
2
💡 Note:
Child with greed factor 1 gets cookie of size 1, child with greed factor 2 gets cookie of size 2. Both children are satisfied.
Example 3 — No Satisfaction
$
Input:
g = [5,6,7], s = [1,2,3]
›
Output:
0
💡 Note:
All children have greed factors larger than any available cookie size, so no child can be satisfied.
Constraints
- 1 ≤ g.length ≤ 3 × 104
- 0 ≤ s.length ≤ 3 × 104
- 1 ≤ g[i] ≤ 231 - 1
- 1 ≤ s[j] ≤ 231 - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
Children with greed factors [1,2,3] and cookies with sizes [1,1]
2
Process
Match children with cookies using greedy strategy
3
Output
Maximum number of satisfied children: 1
Key Takeaway
🎯 Key Insight: Greedy strategy of satisfying least greedy children first maximizes total satisfaction
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code