LeetCode Entry
860. Lemonade Change
Simulate money exchange
860. Lemonade Change easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/703
Problem TLDR
Simulate money exchange #easy #simulation
Intuition
- queue order must not be changed
Just simulate the process.
Approach
- we don’t have to keep $20’s
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun lemonadeChange(bills: IntArray): Boolean {
val s = IntArray(21)
return bills.all { b ->
s[b]++
if (b > 5) s[5]--
if (b > 10) if (s[10] > 0) s[10]-- else s[5] -= 2
s[5] >= 0
}
}
pub fn lemonade_change(bills: Vec<i32>) -> bool {
let (mut s5, mut s10) = (0, 0);
bills.iter().all(|&b| {
if b == 5 { s5 += 1 }
if b == 10 { s10 += 1 }
if b > 5 { s5 -= 1 }
if b > 10 { if s10 > 0 { s10 -= 1 } else { s5 -= 2 }}
s5 >= 0
})
}