0455-分发饼干(0455 – distribution of biscuits)

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

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

示例 1:

输入: g = [1,2,3], s = [1,1]
输出: 1
解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
示例 2:

输入: g = [1,2], s = [1,2,3]
输出: 2
解释:
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.

提示:

1 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <= 231 – 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/assign-cookies

参考:

  • https://leetcode-cn.com/problems/assign-cookies/solution/dai-ma-sui-xiang-lu-dai-ni-xue-tou-tan-x-6jsc/

python

# 0455.分发饼干

class Solution1:
    def findContentChildren(self, g:[int], s:[int]) -> int:
        """
        小饼干优先喂饱小胃口
        :param g:
        :param s:
        :return:
        """
        g.sort() # 小孩胃口排序
        s.sort() # 饼干尺寸排序
        res = 0
        for i in range(len(s)):
            if res < len(g) and s[i] >= g[res]: # 小饼干优先喂饱小胃口
                res += 1
        return res


class Solution2:
    def findContentChildren(self, g:[int], s:[int]) -> int:
        """
        大饼干优先喂饱大胃口
        :param g:
        :param s:
        :return:
        """
        g.sort() # 小孩胃口排序
        s.sort() # 饼干尺寸排序
        start, count = len(s)-1, 0
        for index in range(len(g)-1, -1, -1):
            if start >= 0 and g[index] <= s[start]:
                start -= 1
                count += 1
        return count

golang

package greedy

import "sort"

// 小饼干满足小胃口
func findContentChildren1(g,s []int) int {
	sort.Ints(g)
	sort.Ints(s)
	var res int = 0
	for i:=0;i<len(s);i++ {
		if res < len(g) && s[i] >= g[res] {
			res++
		}
	}
	return res
}

// 大饼干优先满足大胃口
func findContentChildren2(g,s []int) int {
	sort.Ints(g)
	sort.Ints(s)
	start, count := len(s)-1, 0
	for index:=len(g)-1;index>-1;index-- {
		if start >= 0 && g[index] <= s[start] {
			start--
			count++
		}
	}
	return count
}
————————

Suppose you are a great parent and want to give your children some cookies. However, each child can only give one biscuit at most.

For every child I, there is an appetite value   G [i], this is the minimum size of biscuits that can satisfy children’s appetite; And every cookie J has a size s [J]  。 If s [J]  & gt;= G [i], we can distribute this biscuit J to child I, and the child will be satisfied. Your goal is to meet as many children as possible and output this maximum value.

Example   1:

Input: g = [1,2,3], s = [1,1]
Output: 1
Explanation:
You have three children and two biscuits. The appetite values of the three children are: 1, 2 and 3.
Although you have two small biscuits, because their size is 1, you can only satisfy children with an appetite of 1.
So you should output 1.
Example   2:

Input: g = [1,2], s = [1,2,3]
Output: 2
Explanation:
You have two children and three cookies. The appetite values of two children are 1 and 2 respectively.
You have enough cookies and sizes to satisfy all children.
So you should output 2

Tips:

1 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <=   231 – 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/assign-cookies

reference resources:

  • https://leetcode-cn.com/problems/assign-cookies/solution/dai-ma-sui-xiang-lu-dai-ni-xue-tou-tan-x-6jsc/

python

# 0455.分发饼干

class Solution1:
    def findContentChildren(self, g:[int], s:[int]) -> int:
        """
        小饼干优先喂饱小胃口
        :param g:
        :param s:
        :return:
        """
        g.sort() # 小孩胃口排序
        s.sort() # 饼干尺寸排序
        res = 0
        for i in range(len(s)):
            if res < len(g) and s[i] >= g[res]: # 小饼干优先喂饱小胃口
                res += 1
        return res


class Solution2:
    def findContentChildren(self, g:[int], s:[int]) -> int:
        """
        大饼干优先喂饱大胃口
        :param g:
        :param s:
        :return:
        """
        g.sort() # 小孩胃口排序
        s.sort() # 饼干尺寸排序
        start, count = len(s)-1, 0
        for index in range(len(g)-1, -1, -1):
            if start >= 0 and g[index] <= s[start]:
                start -= 1
                count += 1
        return count

golang

package greedy

import "sort"

// 小饼干满足小胃口
func findContentChildren1(g,s []int) int {
	sort.Ints(g)
	sort.Ints(s)
	var res int = 0
	for i:=0;i<len(s);i++ {
		if res < len(g) && s[i] >= g[res] {
			res++
		}
	}
	return res
}

// 大饼干优先满足大胃口
func findContentChildren2(g,s []int) int {
	sort.Ints(g)
	sort.Ints(s)
	start, count := len(s)-1, 0
	for index:=len(g)-1;index>-1;index-- {
		if start >= 0 && g[index] <= s[start] {
			start--
			count++
		}
	}
	return count
}