File: assign-cookies/solution.py

Date: 2026-06-06

Time: 15:17

Assign Cookies — LeetCode 455

Purpose

This file implements the solution to LeetCode 455: Assign Cookies. It solves the problem of maximizing the number of children who each receive a cookie that meets or exceeds their greed factor. It's one solution module among hundreds in the leetcode-implementations repo, following the standard solution.py convention.

Key Components

findcontentchildren(g, s) -> int — The sole public function. Takes two lists:

Returns the maximum number of children that can be satisfied.

Patterns

Greedy two-pointer on sorted arrays. This is the textbook greedy approach for assignment problems where you want to maximize matches under a capacity constraint. The insight: sort both lists, then try to match the least greedy child with the smallest sufficient cookie. If a cookie is too small for the current child, skip it — it won't satisfy any greedier child either.

The algorithm mutates the input lists in-place via .sort() rather than creating sorted copies. This is a deliberate space optimization (O(1) extra space vs O(n) for sorted()), though it means the caller's lists are modified as a side effect.

Dependencies

Imports: None — pure standard library, no external dependencies.

Imported by: assign-cookies/test_solution.py directly. The "Imported By" list in the prompt is misleading — those are all test files for *other* problems that happen to share a common test harness or conftest, not actual importers of this solution's logic.

Flow

1. Sort g (greed factors) ascending — least greedy child first

2. Sort s (cookie sizes) ascending — smallest cookie first

3. Walk both lists with two pointers (child, cookie):

4. Loop terminates when either all children are checked or all cookies are exhausted.

5. Return child — the count of satisfied children.

The child pointer only advances on a successful match, so its final value equals the number of content children.

Invariants

Error Handling

None. The function assumes valid inputs per LeetCode constraints (non-negative integers). Empty lists work correctly — the while-loop body never executes and child returns as 0.