-
Notifications
You must be signed in to change notification settings - Fork 5
/
LemonadeChange.java
80 lines (71 loc) · 2.23 KB
/
LemonadeChange.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Problem Statement:
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 don't have any change in hand at first.
Return true if and only if you can provide every customer with correct change.
Problem Link:
Lemonade Change:https://leetcode.com/problems/lemonade-change/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/lemonadeChange.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
int five=0, ten=0, twenty=0;
public boolean lemonadeChange(int[] bills) {
for(int i =0;i<bills.length;i++){
int value = bills[i];
if(value==5)
five++;
if(value==10)
ten++;
if(value==20)
twenty++;
value -= 5;
if(!checkPossible(value))
return false;
}
return true;
}
public boolean checkPossible(int value){
////////////15////////////
if(value==15 && ten>=1){
ten--;
value-=10;
}
else if(value==15 && five>=3){
five -=3;
return true;
}
////////////10////////////
if(value==10 && ten>=1){
ten--;
return true;
}
else if(value==10 && five>=2){
five -= 2;
value -=10;
return true;
}
else if(value==10){
return false;
}
////////////5////////////
if(value==5 && five<=0){
return false;
}
else if(value==5){
five--;
value=0;
return true;
}
if(value!=0)
return false;
return true;
}
}