860. Lemonade Change

Posted johnnyzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了860. Lemonade Change相关的知识,希望对你有一定的参考价值。

/**
 * 860. Lemonade Change
 * https://leetcode.com/problems/lemonade-change/description/
 * */
class Solution {
    //使用贪心,策略是每次找钱选择大面额,尽量保留5元的
    fun lemonadeChange(bills: IntArray): Boolean {
        var fiveCount = 0
        var tenCount = 0
        for (bill in bills) {
            if (bill == 5) {
                fiveCount++
            } else if (bill == 10) {
                tenCount++
                fiveCount--
            } else if (tenCount > 0) {
                //if can change for 10, give 10 and 5
                fiveCount--
                tenCount--
            } else {
                //just can give back 5
                fiveCount -= 3
            }
            if (fiveCount < 0) {
                return false
            }
        }
        return true
    }
}

 

以上是关于860. Lemonade Change的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode_easy860. Lemonade Change

leetcode 860. Lemonade Change

[LeetCode] 860. Lemonade Change

[LeetCode] 860. Lemonade Change

860. Lemonade Change

860. Lemonade Change