367. Assign Cookies
You have a line of children and a bag of cookies. Child i has an appetite g[i]: the smallest cookie size that will satisfy them. Cookie j has size s[j].
Each child receives at most one cookie, each cookie goes to at most one child, and child i ends up content only if their cookie satisfies s[j] >= g[i]. Hand out cookies to make as many children content as possible.
Your function receives the appetite array g and the cookie-size array s (which may be empty) and returns the maximum number of content children.
Example 1:
Input: g = [1, 2, 3], s = [1, 1]
Output: 1
Explanation: Both cookies have size 1, which only satisfies the child with appetite 1. The other two children get nothing they'd accept.
Example 2:
Input: g = [1, 2], s = [1, 2, 3]
Output: 2
Explanation: Give the size-1 cookie to the appetite-1 child and the size-2 cookie to the appetite-2 child. Every child is content.
Constraints:
- 1 ≤ g.length ≤ 3 * 10⁴
- 0 ≤ s.length ≤ 3 * 10⁴
- 1 ≤ g[i], s[j] ≤ 10⁹
Hints:
A big cookie can satisfy anyone a small cookie can — so wasting a big cookie on an easily-pleased child only ever hurts. What order should you consider children and cookies in?
Sort both arrays ascending and sweep the cookies once: point at the easiest unfed child; if the current cookie satisfies them, feed and advance the child pointer, otherwise discard the cookie — it satisfies no one who is left.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: g = [1, 2, 3], s = [1, 1]
Expected output: 1