Assign Cookies

You want to give cookies to children, at most one cookie each. Child i has a greed factor g[i], the smallest cookie size that will make them content, and cookie j has size s[j]. Child i is content if given a cookie with s[j] >= g[i]. Return the maximum number of content children.

30:00

Assign Cookies
easy
Free written solution
Topics
Companies

You want to give cookies to children, at most one cookie each. Child i has a greed factor g[i], the smallest cookie size that will make them content, and cookie j has size s[j]. Child i is content if given a cookie with s[j] >= g[i]. Return the maximum number of content children.

Example 1:
Input: {"g":[1,2,3],"s":[1,1]}
Output: 1
Constraints:
  • 1≤g.length≤3×1041 \leq \text{g.length} \leq 3 \times 10^4

  • 0≤s.length≤3×1040 \leq \text{s.length} \leq 3 \times 10^4

  • 1≤g[i],s[j]≤231−11 \leq g[i], s[j] \leq 2^{31} - 1

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"g":[1,2,3],"s":[1,1]}

3 children and 2 cookies, in no particular order yet.

Children by greed (unsorted)

content = 0

1

#0

2

#1

3

#2

Cookies by size (unsorted)

1

s0

1

s1

given to a child

too small / not needed

least greedy hungry child

Variables
VariableValue
child0
DepthFunction Call
Stack empty
0/5