Match Each Guest With a Snack Big Enough

Implement maxHappyGuests

A host has several guests and several snacks. A guest is satisfied by any single snack whose size is at least their appetite. Each guest can be given at most one snack, and each snack can go to at most one guest. Find the largest number of guests that can be satisfied.

Trying every possible hand-out works, but the number of possibilities explodes. A greedy rule cuts it down: sort both lists, and always serve the least hungry remaining guest with the smallest snack that can do the job.

Example 1:

Input: appetite = [4,1,6,3], snacks = [2,5,3]

Output: 3

Example 2:

Input: appetite = [7], snacks = [5]

Output: 0

Example 3:

Input: appetite = [2,2], snacks = [9,9,9]

Output: 2

+ 9 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ appetite.length ≤ 8 and 1 ≤ snacks.length ≤ 8
  • ●1 ≤ appetite[i], snacks[j] ≤ 50
  • ●A guest with appetite a is satisfied by any single snack of size ≥ a; each guest gets at most one snack and each snack goes to at most one guest
  • ●Return the largest number of guests that can be satisfied

appetite =

[4, 1, 6, 3]

snacks =

[2, 5, 3]