每日一题力扣455 小饼干和孩子

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。

对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/assign-cookies
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        #先排序再分饼干,g是孩子,s是饼干
        g.sort()
        s.sort()
        m=len(g)
        n=len(s)
        count=0
        i=0
        j=0
        while i <m and j<n:
            while j<n and g[i]>s[j]:
                j+=1#不满足胃口,那么对于这个孩子看下一块小饼干
            if j<n:#如果遍历小饼干到了最后找到了,那么就count小饼干,表示分配到了
                count+=1
            i+=1
            j+=1
        return count
原文地址:https://www.cnblogs.com/liuxiangyan/p/14603953.html