Assign Cookies

Basic Greedy Problems DSA practice problem on Onlearn.

Difficulty: easy.

Topics: How to maximize the number of content children by assigning cookies based on their greed factors and available cookie sizes., Arrays, Sorting, Two Pointers, Greedy Algorithm, greedy algorithm, sorting algorithms, array traversal, time complexity analysis, two pointer technique.

Problem Statement You are given two integer arrays g (children's greed factors) and s (cookie sizes). Each child i has a greed factor g[i], which is the minimum size of a cookie required to satisfy them. Each cookie j has a size s[j]. A child i can be satisfied if there exists a cookie j such that s[j] = g[i], and each cookie can be assigned to at most one child. Your task is to maximize the number of satisfied children. Input Format Two integer arrays g and s (space separated if read as strings) Output Format An integer representing the maximum number of satisfied children Constraints 1 <= g.length <= 3 10^4 1 <= s.length <= 3 10^4 1 <= g[i], s[j] <= 2^31 1 Examples Example 1: Input: g = [1, 2, 3], s = [1, 1] Output: 1 Explanation: Only the child with greed factor 1 can be satisfied by a cookie of size 1. Example 2: Input: g = [1, 5, 3, 3, 4], s = [4, 2, 1, 2, 1, 3] Output: 3 Explanation: Children with greed factors 1, 3, and 4 can be satisfied by cookies of sizes 1, 3, and 4 respectively. Difficulty Level : Easy