Ctrl K

Cake Slices for Guests

Easy
Guests have arrived at a party, and each has their own appetite: guest i is happy only with a slice of size at least appetites[i]. The cake is already cut, and the slice sizes are listed in slices. Each guest gets at most one slice, and each slice goes to at most one guest. Hand out the slices so that as many guests as possible are happy, and return how many that is.
Example 1
appetites=[4, 2, 6], slices=[3, 5, 1] → 2

Slice 3 goes to the guest with appetite 2 and slice 5 to the guest with appetite 4. Slice 1 is too small for anyone, and no slice is big enough for appetite 6: two guests are happy.

Example 2
appetites=[3, 3], slices=[] → 0

There is no cake, so nobody is happy.

Constraints
  • 1 ≤ length of appetites ≤ 3·10⁴
  • 0 ≤ length of slices ≤ 3·10⁴
  • 1 ≤ appetites[i], slices[j] ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumFix the Middle of the Line MediumStick Triples That Form a Triangle MediumBest-Paid Gig for Each Worker MediumLining Up Against Rivals Medium
loading Python…
Input
appetites=[4, 2, 6], slices=[3, 5, 1]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 126 tests on the server, hidden ones included.