-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path455-分发饼干.py
59 lines (50 loc) · 1.8 KB
/
455-分发饼干.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- encoding: utf-8 -*-
'''
@File : 455-分发饼干.py
@Time : 2021/07/02 20:13:56
@Author : TYUT ltf
@Version : v1.0
@Contact : [email protected]
@License : (C)Copyright 2020-2030, GNU General Public License
'''
# here put the import lib
from typing import List
'''
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
对每个孩子 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.
'''
class Solution:
def findContentChildren(self, g: List[int], s: List[int]):
g.sort()
s.sort()
g_point = len(g)-1
s_point = len(s)-1
count = 0
# 当g_point 或者 s_point = 0 时候循环结束
while g_point >= 0 and s_point >= 0:
if s[s_point] >= g[g_point]:
count += 1
g_point -= 1
s_point -= 1
else:
g_point -= 1
return count
obj = Solution()
g = [1, 2]
s = [1, 2, 3]
print(obj.findContentChildren(g, s))