bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 455. Assign Cookies

455. Assign Cookies

Leetcode Solutions

Greedy Approach with Sorting

  1. Sort the greed factors array g in ascending order.
  2. Sort the cookie sizes array s in ascending order.
  3. Initialize two pointers, childIndex and cookieIndex, to 0. These will track the current child and cookie being considered.
  4. Initialize a variable contentChildren to 0 to count the number of content children.
  5. While childIndex is less than the length of g and cookieIndex is less than the length of s, do the following: a. If the current cookie size s[cookieIndex] is greater than or equal to the current child's greed factor g[childIndex], increment contentChildren, move to the next child by incrementing childIndex, and consider the next cookie by incrementing cookieIndex. b. If the current cookie size is smaller than the child's greed factor, move to the next cookie by incrementing cookieIndex.
  6. Once we have either run out of children or cookies, return the count of contentChildren.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR