diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /assign_cookies.py | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'assign_cookies.py')
-rw-r--r-- | assign_cookies.py | 37 |
1 files changed, 37 insertions, 0 deletions
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 |