From d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa Mon Sep 17 00:00:00 2001 From: jack <0x6A73@pm.me> Date: Fri, 14 Jun 2024 13:13:15 -0600 Subject: initial --- assign_cookies.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 assign_cookies.py (limited to 'assign_cookies.py') diff --git a/assign_cookies.py b/assign_cookies.py new file mode 100644 index 0000000..87e4f68 --- /dev/null +++ b/assign_cookies.py @@ -0,0 +1,37 @@ +# 455. Assign Cookies + +""" +assume you are an awesome parent and want to give your children some cookies. +but you should give each child at most one cookie. each child 'i' has a greed +factor 'g[i]', which is the minimum size of a cookie that the child will be +content with; and each cookie 'j' has a size 's[j]' if 's[j] >= g[i]', we can +assign the cookie 'j' to the child 'i', and the child 'i' will be content. +your goal is to maximise the number of your content children and output the +maximum number. +""" + + +class Solution(object): + def findContentChildren(self, g, s): + """ + :type g: List[int] + :type s: List[int] + :rtype: int + """ + g.sort(reverse=True) + s.sort(reverse=True) + res, i, j = 0, 0, 0 + while i < len(g) and j < len(s): + if g[i] <= s[j]: + res += 1 + i += 1 + j += 1 + else: + i += 1 + return res + + +if __name__ == "__main__": + obj = Solution() + print(obj.findContentChildren(g=[1, 2, 3], s=[1, 1])) # expect: 1 + print(obj.findContentChildren(g=[1, 2], s=[1, 2, 3])) # expect: 2 -- cgit v1.2.3