Skip to main content
Back to problems
Leetcode
Easy
Arrays
Greedy
Sorting
Assign Cookies

Assign cookies to as many children as possible so that each satisfied child gets a cookie at least as large as their greed factor.

Acceptance 100%
Problem Statement

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 g and s
    • g[i] is the greed factor of the i-th child
    • s[j] is the size of the j-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.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.