Assign Cookies

Given two integer arrays g and s, where g[i] is the greed factor of the ith child and s[j] is the size of the jth cookie, return the maximum number of children that can be content. A child is content if assigned a cookie with size greater than or equal to its greed factor. Pattern focus: Greedy Choice Property. Sort both arrays and always satisfy the least greedy child first so that larger cookies remain available for later children.

Input Format

g = greed factors of children, s = cookie sizes

Output Format

maximum number of content children

Constraints

  • 1 <= g.length, s.length <= 10^5
  • 1 <= g[i], s[j] <= 10^9

Examples

Example 1:

Input:

g = [1,2,3]
s = [1,1]

Output:

1

Explanation:

Only one child can be satisfied because there are only two cookies of size 1.

Example 2:

Input:

g = [1,2]
s = [1,2,3]

Output:

2

Explanation:

Both children can be satisfied by assigning the smallest feasible cookies first.

Example 3:

Input:

g = [2,3,4]
s = [1,1,1]

Output:

0

Explanation:

No cookie is large enough to satisfy any child.

Loading...
Assign Cookies - Greedy DSA Problem