Problem 594359 · easy · Phase 05 Advanced Algorithms & Graphs

Snack Packs That Are Not Too Big

greedy · two pointers · sorting

Child i has appetite appetite[i]. A snack pack of size s makes child i happy only if appetite[i] <= s <= 2 * appetite[i] (big enough, but not so big that it is wasted). Each child gets at most one pack and each pack goes to at most one child. Return the maximum number of happy children.

Examples

Input:  appetite = [2, 3, 10], packs = [1, 4, 5, 30]
Output: 2
Explanation: 4 -> the child with appetite 2, 5 -> the child with appetite 3. 30 is too big for 10.
Input:  appetite = [4, 4], packs = [8, 4]
Output: 2

Constraints

  • 0 <= len(appetite), len(packs) <= 10**5, all values between 1 and 10**9.
  • Target complexity: O(n log n + m log m).

Goals

  • Sort both lists so a single forward sweep can match them
  • Discard packs that are too small for everyone still waiting
  • Handle the upper size limit that makes a pack unusable for a child
Starting Python…