[
greedy
two pointers
]
Leetcode 0455 Assign Cookies
Problem statement
https://leetcode.com/problems/assign-cookies/
Solution
Sort array of greed and array of cookies sizes in ascending order in O(n log n + m log m)
and then use two iterators to feed children with lowest greed.
Complexity
Time complexity is O(n log n + m log m)
, space complexity is O(m + n)
.
Code
class Solution:
def findContentChildren(self, g, s):
g, s = sorted(g), sorted(s)
i, j, ans = 0, 0, 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
i += 1
ans += 1
j += 1
return ans