โ† Back to Index

๐Ÿ“š Assign Cookies โ€“ Java Cheat Sheet

๐Ÿ“Œ What Is It?

The Assign Cookies problem involves distributing cookies to children such that the maximum number of children are satisfied. Each child has a greed factor, and each cookie has a size. A child is satisfied if the size of the cookie is greater than or equal to their greed factor.

๐Ÿงฑ Pattern Template

Greedy Algorithm

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g); // Sort greed factors
        Arrays.sort(s); // Sort cookie sizes
        int child = 0, cookie = 0;

        while (child < g.length && cookie < s.length) {
            if (s[cookie] >= g[child]) {
                child++; // Satisfy this child
            }
            cookie++; // Move to the next cookie
        }

        return child; // Number of satisfied children
    }
}

๐Ÿ“Š Time Complexity

โœ… Use Cases

๐Ÿ“˜ Common LeetCode Problems

๐Ÿงช Example

Input: g = [1,2,3], s = [1,1]
Output: 1

Explanation:
- Only 1 child can be satisfied with the given cookies.

๐Ÿ’ก Pro Tips