钞票支付问题:
有1元、5元、10元、20元、100元、200元面额的钞票无穷张,现使用这些钞票支付628元,需要多少张?
最佳的支付方式:3张200元,1张20元,1张5元,3张1元。
直觉告诉我们:尽可能多的使用面值较大的钞票。
- 简单直觉的定义:遵循某种规律,不断贪心的选取当前最优的策略。
- PS: 但是这种贪心策略不一定正确,如果加上一种7元面值的纸币,上述的贪心策略就不对了,所以贪心算法一般会配合动态规划一起考虑
LeetCode455:
题意和实例
思路:
思路
贪心规律:
贪心规律
伪代码
Python代码:
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
child = 0
cookie = 0
g.sort()
s.sort()
while child < len(g) and cookie < len(s):
if g[child] <= s[cookie]:
child += 1
cookie += 1
return child