贪心
# 860. 柠檬水找零 - 力扣(LeetCode) (opens new window)
class Solution {
public boolean lemonadeChange(int[] bills) {
int crash_5 = 0;
int crash_10 = 0;
for(int i = 0; i < bills.length; i++){
if (bills[i] == 5){
crash_5++;
} else if (bills[i] == 10){
crash_5--;
crash_10++;
} else if (bills[i] == 20){
if(crash_10 > 0){
crash_10--;
crash_5--;
} else {
crash_5 -= 3;
}
}
if (crash_5 < 0 || crash_10 < 0) return false;
}
return true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
编辑 (opens new window)
上次更新: 2025/06/13, 00:51:28