Skip to content

Commit 83911d2

Browse files
add 860
1 parent d941657 commit 83911d2

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Your ideas/fixes/algorithms are more than welcome!
5858
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | O(n) | O(h) | |Easy| DFS, recursion
5959
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java) | O(n) | O(n) | |Easy|
6060
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | O(r*c) | O(r*c) | |Easy|
61+
|860|[Lemonade Change](https://leetcode.com/problems/lemonade-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_860.java) | O(n) | O(1) | |Easy|
6162
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | O(n) | O(n) | |Easy|
6263
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
6364
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 860. Lemonade Change
8+
*
9+
* At a lemonade stand, each lemonade costs $5.
10+
*
11+
* Customers are standing in a queue to buy from you,
12+
* and order one at a time (in the order specified by bills).
13+
* Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill.
14+
* You must provide the correct change to each customer,
15+
* so that the net transaction is that the customer pays $5.
16+
* Note that you don't have any change in hand at first.
17+
* Return true if and only if you can provide every customer with correct change.
18+
*
19+
* Example 1:
20+
* Input: [5,5,5,10,20]
21+
* Output: true
22+
* Explanation:
23+
* From the first 3 customers, we collect three $5 bills in order.
24+
* From the fourth customer, we collect a $10 bill and give back a $5.
25+
* From the fifth customer, we give a $10 bill and a $5 bill.
26+
* Since all customers got correct change, we output true.
27+
*
28+
* Example 2:
29+
* Input: [5,5,10]
30+
* Output: true
31+
*
32+
* Example 3:
33+
* Input: [10,10]
34+
* Output: false
35+
*
36+
* Example 4:
37+
* Input: [5,5,10,10,20]
38+
* Output: false
39+
* Explanation:
40+
* From the first two customers in order, we collect two $5 bills.
41+
* For the next two customers in order, we collect a $10 bill and give back a $5 bill.
42+
* For the last customer, we can't give change of $15 back because we only have two $10 bills.
43+
* Since not every customer received correct change, the answer is false.
44+
*
45+
*
46+
* Note:
47+
* 0 <= bills.length <= 10000
48+
* bills[i] will be either 5, 10, or 20.
49+
*/
50+
public class _860 {
51+
public static class Solution1 {
52+
public boolean lemonadeChange(int[] bills) {
53+
Map<Integer, Integer> map = new HashMap<>();
54+
for (int bill : bills) {
55+
if (bill == 5) {
56+
map.put(5, map.getOrDefault(5, 0) + 1);
57+
} else if (bill == 10) {
58+
if (!map.containsKey(5)) {
59+
return false;
60+
} else {
61+
map.put(5, map.get(5) - 1);
62+
if (map.get(5) == 0) {
63+
map.remove(5);
64+
}
65+
map.put(10, map.getOrDefault(10, 0) + 1);
66+
}
67+
} else {
68+
if (!map.containsKey(5)) {
69+
return false;
70+
} else {
71+
if (!map.containsKey(10)) {
72+
if (!map.containsKey(5) || map.get(5) < 3) {
73+
return false;
74+
} else {
75+
map.put(5, map.get(5) - 3);
76+
if (map.get(5) == 0) {
77+
map.remove(5);
78+
}
79+
}
80+
} else {
81+
if (!map.containsKey(5)) {
82+
return false;
83+
} else {
84+
map.put(5, map.get(5) - 1);
85+
if (map.get(5) == 0) {
86+
map.remove(5);
87+
}
88+
map.put(10, map.get(10) - 1);
89+
if (map.get(10) == 0) {
90+
map.remove(10);
91+
}
92+
}
93+
}
94+
}
95+
map.put(20, map.getOrDefault(20, 0) + 1);
96+
}
97+
}
98+
return true;
99+
}
100+
}
101+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._860;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _860Test {
10+
11+
private static _860.Solution1 test;
12+
private static int[] bills;
13+
14+
@BeforeClass
15+
public static void setUp() {
16+
test = new _860.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
bills = new int[] {5, 5, 5, 10, 20};
22+
assertEquals(true, test.lemonadeChange(bills));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
bills = new int[] {5, 5, 10};
28+
assertEquals(true, test.lemonadeChange(bills));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
bills = new int[] {10, 10};
34+
assertEquals(false, test.lemonadeChange(bills));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
bills = new int[] {5, 5, 10, 10, 20};
40+
assertEquals(false, test.lemonadeChange(bills));
41+
}
42+
43+
@Test
44+
public void test5() {
45+
bills = new int[] {5, 5, 5, 20, 5, 5, 5, 10, 20, 5, 10, 20, 5, 20, 5, 10, 5, 5, 5, 5};
46+
assertEquals(false, test.lemonadeChange(bills));
47+
}
48+
}

0 commit comments

Comments
 (0)