Assign Cookies
Updated
Problem statement
You want to give cookies to children. greed[i] is the smallest cookie size that makes child i happy, and cookies[j] is the size of cookie j.
- Each child gets at most one cookie, and each cookie goes to at most one child.
- A child is happy only when the cookie they get has a size of at least their greed.
- The arrays can be in any order, and a cookie may stay unused.
Return the largest number of children you can make happy.
Function
assignCookies(greed: int[], cookies: int[]) → intExamples
Example 1
greed = [4,1,2]cookies = [1,3]return = 2The child with greed 1 takes the cookie of size 1, and the child with greed 2 takes the cookie of size 3. No cookie is left for the child with greed 4, so 2 children are happy.
Example 2
greed = [1,2]cookies = [2,1]return = 2Give size 1 to greed 1 and size 2 to greed 2. If you gave the largest cookie to the smallest child, the child with greed 2 would be left with size 1, which is too small, and only 1 child would be happy.
Example 3
greed = [2,3]cookies = [1,2,3]return = 2The cookie of size 1 is too small for every child, so it stays unused. Size 2 goes to greed 2, and size 3 goes to greed 3.
Example 4
greed = [5,5]cookies = []return = 0There are no cookies, so no child is happy.
Constraints
1 <= greed.length <= 3 * 10^40 <= cookies.length <= 3 * 10^41 <= greed[i], cookies[j] <= 2^31 - 1
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
- Minimum Tunnel Crossing TimeArcesium · OAArcesium interview questions
- Get Maximum PairsAmazon · OA
- Find Maximum GreatnessTwilio · OATwilio interview questions
- Binge WatchingVanguard · OAVanguard interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Minimum Removals to Balance ArraySalesforce · OASalesforce interview questions