Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Assign Cookies

Greedyeasy

Given children greed factors `g` and cookie sizes `s`, implement `findContentChildren(g, s)` returning the maximum number of children that can each get a cookie at least as large as their greed.

Sample tests

Input: findContentChildren([1,2,3], [1,1])
Output: 1
Input: findContentChildren([1,2], [1,2,3])
Output: 2

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Matching without sorting wastes large cookies on small needs.

Learning resources

  • Wikipedia: Greedy algorithm
Approach & explanation (try first)

Sorting and a two-pointer match maximizes content children in O(n log n) time.

Loading...
⌘/Ctrl + Enter

Run your code to see results.