aboutsummaryrefslogtreecommitdiff
path: root/assign_cookies.py
blob: 87e4f684e131956a4373d765fa087df061e74f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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