-
Notifications
You must be signed in to change notification settings - Fork 0
/
s0860_lemonade_change.rs
52 lines (48 loc) · 1.53 KB
/
s0860_lemonade_change.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
struct Solution;
//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
pub fn lemonade_change(bills: Vec<i32>) -> bool {
let mut b5 = 0;
let mut b10 = 0;
for bill in bills {
match (bill, b5 > 0, b10 > 0, b5 > 2) {
(5, _, _, _) => b5 += 1,
(10, true, _, _) => {
b10 += 1;
b5 -= 1;
}
(20, true, true, _) => {
b10 -= 1;
b5 -= 1;
}
(20, _, _, true) => {
b5 -= 3;
}
_ => return false,
}
}
true
}
}
//leetcode submit region end(Prohibit modification and deletion)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let bills = vec![5, 5, 5, 10, 20];
assert_eq!(Solution::lemonade_change(bills), true);
let bills = vec![5, 5, 10];
assert_eq!(Solution::lemonade_change(bills), true);
let bills = vec![10, 10];
assert_eq!(Solution::lemonade_change(bills), false);
let bills = vec![5, 5, 10, 10, 20];
assert_eq!(Solution::lemonade_change(bills), false);
let bills = vec![
5, 5, 10, 20, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 5, 5, 20, 5, 20, 5,
];
assert_eq!(Solution::lemonade_change(bills), true);
let bills = vec![5, 5, 5, 5, 20, 20, 5, 5, 5, 5];
assert_eq!(Solution::lemonade_change(bills), false);
}
}