Skip to content

Commit 51cd400

Browse files
committed
solve problem Lemonade Change
1 parent 7ecb8cc commit 51cd400

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ All solutions will be accepted!
170170
|190|[Reverse Bits](https://leetcode-cn.com/problems/reverse-bits/description/)|[java/py/js](./algorithms/ReverseBits)|Easy|
171171
|400|[Nth Digits](https://leetcode-cn.com/problems/nth-digit/description/)|[java/py/js](./algorithms/NthDigits)|Easy|
172172
|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|
173174

174175
# Database
175176
|#|Title|Solution|Difficulty|

algorithms/LemonadeChange/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Lemonade Change
2+
This problem is easy to solve
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

algorithms/LemonadeChange/solution.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

algorithms/LemonadeChange/solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

0 commit comments

Comments
 (0)