File tree 5 files changed +103
-0
lines changed
algorithms/LemonadeChange
5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,7 @@ All solutions will be accepted!
170
170
| 190| [ Reverse Bits] ( https://leetcode-cn.com/problems/reverse-bits/description/ ) | [ java/py/js] ( ./algorithms/ReverseBits ) | Easy|
171
171
| 400| [ Nth Digits] ( https://leetcode-cn.com/problems/nth-digit/description/ ) | [ java/py/js] ( ./algorithms/NthDigits ) | Easy|
172
172
| 160| [ Intersection Of Two Linked Lists] ( https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/ ) | [ java/py/js] ( ./algorithms/IntersectionOfLinkedLists ) | Easy|
173
+ | 860| [ LemonadeChange] ( https://leetcode-cn.com/problems/lemonade-change/description/ ) | [ java/py/js] ( ./algorithms/LemonadeChange ) | Easy|
173
174
174
175
# Database
175
176
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Lemonade Change
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean lemonadeChange (int [] bills ) {
3
+ Map <Integer , Integer > remains = new HashMap <Integer , Integer >();
4
+ remains .put (5 , 0 );
5
+ remains .put (10 , 0 );
6
+
7
+ for (int bill : bills ) {
8
+ int remains_5 = remains .get (5 ),
9
+ remains_10 = remains .get (10 );
10
+
11
+ if (bill == 5 ) {
12
+ remains .put (5 , remains_5 + 1 );
13
+ } else if (bill == 10 ) {
14
+ if (remains_5 > 0 ) {
15
+ remains .put (5 , remains_5 - 1 );
16
+ remains .put (10 , remains_10 + 1 );
17
+ } else {
18
+ return false ;
19
+ }
20
+ } else if (bill == 20 ) {
21
+ if (remains_10 > 0 && remains_5 > 0 ) {
22
+ remains .put (5 , remains_5 - 1 );
23
+ remains .put (10 , remains_10 - 1 );
24
+ } else if (remains_5 >= 3 ) {
25
+ remains .put (5 , remains_5 - 3 );
26
+ } else {
27
+ return false ;
28
+ }
29
+ }
30
+ }
31
+
32
+ return true ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } bills
3
+ * @return {boolean }
4
+ */
5
+ var lemonadeChange = function ( bills ) {
6
+ let remains = {
7
+ 5 : 0 ,
8
+ 10 : 0
9
+ }
10
+
11
+ for ( let i = 0 ; i < bills . length ; i ++ ) {
12
+ let bill = bills [ i ]
13
+
14
+ if ( bill === 5 ) {
15
+ remains [ 5 ] ++
16
+ } else if ( bill === 10 ) {
17
+ if ( remains [ 5 ] > 0 ) {
18
+ remains [ 5 ] --
19
+ remains [ 10 ] ++
20
+ } else {
21
+ return false
22
+ }
23
+ } else if ( bill === 20 ) {
24
+ if ( remains [ 10 ] > 0 && remains [ 5 ] > 0 ) {
25
+ remains [ 5 ] --
26
+ remains [ 10 ] --
27
+ } else if ( remains [ 5 ] >= 3 ) {
28
+ remains [ 5 ] -= 3
29
+ } else {
30
+ return false
31
+ }
32
+ }
33
+ }
34
+
35
+ return true
36
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def lemonadeChange (self , bills ):
3
+ """
4
+ :type bills: List[int]
5
+ :rtype: bool
6
+ """
7
+ remains = {
8
+ 5 : 0 ,
9
+ 10 : 0
10
+ }
11
+
12
+ for bill in bills :
13
+ if bill == 5 :
14
+ remains [5 ] += 1
15
+ elif bill == 10 :
16
+ if remains [5 ] > 0 :
17
+ remains [5 ] -= 1
18
+ remains [10 ] += 1
19
+ else :
20
+ return False
21
+ elif bill == 20 :
22
+ if remains [10 ] > 0 and remains [5 ] > 0 :
23
+ remains [5 ] -= 1
24
+ remains [10 ] -= 1
25
+ elif remains [5 ] >= 3 :
26
+ remains [5 ] -= 3
27
+ else :
28
+ return False
29
+
30
+ return True
You can’t perform that action at this time.
0 commit comments