美文网首页
37.LeetCode860. 柠檬水找零

37.LeetCode860. 柠檬水找零

作者: 月牙眼的楼下小黑 | 来源:发表于2018-10-04 09:54 被阅读14次
  • 标签: 贪心
  • 难度: 简单

  • 题目描述
  • 我的解法

用一个散列表 gots 作为记账本,键为钞额, 值为当前收到的张数, 找零的贪心策略是: 尽可能地保留 5 元面额的钞票。比如收到 20 元时, 找零时既可以返还 10 + 5 也可以返还 5 + 5 + 5, 但是我们为了尽可能多地保留 5 元钞, 如果条件允许我们优先返还 10 + 5.

class Solution(object):
    def lemonadeChange(self, bills):
        """
        :type bills: List[int]
        :rtype: bool
        """
        gots = {'5':0, '10':0, '20':0}
        for m in bills:
            if (m==5):
                gots['5'] += 1
                continue
            if(m ==10):
                if(gots['5'] >=1):
                    gots['5'] -= 1
                    gots['10'] += 1
                    continue
                else:
                    return False
            if(m==20):
                if(gots['5'] >=1 and gots['10'] >= 1):
                    gots['5'] -= 1
                    gots['10'] -= 1
                    gots['20'] += 1
                    continue
                if(gots['5'] >= 3):
                    gots['5'] -= 3
                    gots['20'] += 1
                    continue
                return False
        return True
                
  • 其他解法

暂略。

相关文章

  • 37.LeetCode860. 柠檬水找零

    标签: 贪心 难度: 简单 题目描述 我的解法 用一个散列表 gots 作为记账本,键为钞额, 值为当前收到的...

  • 每日一题20201210(860. 柠檬水找零)

    860. 柠檬水找零[https://leetcode-cn.com/problems/lemonade-chan...

  • 柠檬水找零

    题目: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次...

  • 柠檬水找零

    题目 在柠檬水摊上,每一杯柠檬水的售价为 5 美元。 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次...

  • 一起学算法-860. 柠檬水找零

    一、题目 LeetCode-860. 柠檬水找零地址:https://leetcode-cn.com/proble...

  • 【LeetCode】柠檬水找零

    题目描述: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)...

  • Day 65: 柠檬水找零

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元。 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一...

  • leetcode 860 柠檬水找零

    这题怎么说,维护五元钞票和十元钞票个数,判断能不能找开就好,纯业务代码!!!可以尝试下dp,题目改一下运行下个人提...

  • 贪心十四:柠檬水找零

    题目地址: https://leetcode-cn.com/problems/lemonade-change/[...

  • LeetCode 柠檬水找零问题

    Record my own stupidity. 20200514 自己的沙雕写法: 因为用到了list.inde...

网友评论

      本文标题:37.LeetCode860. 柠檬水找零

      本文链接:https://www.haomeiwen.com/subject/cfqxaftx.html