Assign cookies to as many children as possible so that each satisfied child gets a cookie at least as large as their greed factor.
You are given two integer arrays: one represents the greed factor of each child, and the other represents the size of each cookie. A child is satisfied only if they receive a cookie whose size is greater than or equal to their greed factor.
Each cookie can be given to at most one child, and each child can receive at most one cookie. Your task is to determine the maximum number of children that can be satisfied.
Input Format
- Two integer arrays
gandsg[i]is the greed factor of thei-th childs[j]is the size of thej-th cookie
Output Format
- Return an integer: the maximum number of children that can be satisfied.
Constraints
1 <= g.length, s.length <= $10^{4}$0 <= g[i], s[j] <= $2^{31}-1$- Each child can receive at most one cookie.
- Each cookie can be used at most once.
Example 1
Input
g = [1,2,3], s = [1,1]
Output
1
Explanation
Only one child can be satisfied. Give one cookie of size 1 to the child with greed factor 1; the other cookies are too small for the remaining children.
Example 2
Input
g = [1,2], s = [1,2,3]
Output
2
Explanation
Assign cookie 1 to the child with greed 1, and cookie 2 to the child with greed 2. The cookie of size 3 is unused.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.