/
uizadcz
/
Algorithm
Обзор
Документация
Войти
/
uizadcz
/
Algorithm
Код
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
LemonadeChange.kt
60 строк
2 KB
ursa
LemonadeChange.kt
25 сен 2024, 14:46
25 сен 2024, 14:46
ba26224
Код
Авторство
О чём код?
/** * https://leetcode.com/problems/lemonade-change/description/ * * At a lemonade stand, each lemonade costs $5. * Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). * Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. * You must provide the correct change to each customer so that the net transaction is that the customer pays $5. * * Note that you do not have any change in hand at first. * * Given an integer array bills where bills[i] is the bill the ith customer pays, * return true if you can provide every customer with the correct change, or false otherwise. */ class LemonadeChange { fun lemonadeChange(bills: IntArray): Boolean { var ans = true var fives = 0 var tens = 0 bills.forEach { bill -> when (bill) { 5 -> { fives++ } 10 -> { if (fives > 0) { fives-- tens++ } else { ans = false return@forEach } } 20 -> { when { fives > 0 && tens > 0 -> { fives-- tens-- } fives >= 3 -> { fives -= 3 } else -> { ans = false return@forEach } } } } } return ans } }