Skip to content

Commit 074f796

Browse files
refactor 860
1 parent 651cb02 commit 074f796

File tree

1 file changed

+46
-90
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+46
-90
lines changed

src/main/java/com/fishercoder/solutions/_860.java

+46-90
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,55 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

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-
*/
506
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);
7+
public static class Solution1 {
8+
public boolean lemonadeChange(int[] bills) {
9+
Map<Integer, Integer> map = new HashMap<>();
10+
for (int bill : bills) {
11+
if (bill == 5) {
12+
map.put(5, map.getOrDefault(5, 0) + 1);
13+
} else if (bill == 10) {
14+
if (!map.containsKey(5)) {
15+
return false;
16+
} else {
17+
map.put(5, map.get(5) - 1);
18+
if (map.get(5) == 0) {
19+
map.remove(5);
20+
}
21+
map.put(10, map.getOrDefault(10, 0) + 1);
22+
}
23+
} else {
24+
if (!map.containsKey(5)) {
25+
return false;
26+
} else {
27+
if (!map.containsKey(10)) {
28+
if (!map.containsKey(5) || map.get(5) < 3) {
29+
return false;
30+
} else {
31+
map.put(5, map.get(5) - 3);
32+
if (map.get(5) == 0) {
33+
map.remove(5);
34+
}
35+
}
36+
} else {
37+
if (!map.containsKey(5)) {
38+
return false;
39+
} else {
40+
map.put(5, map.get(5) - 1);
41+
if (map.get(5) == 0) {
42+
map.remove(5);
43+
}
44+
map.put(10, map.get(10) - 1);
45+
if (map.get(10) == 0) {
46+
map.remove(10);
47+
}
48+
}
49+
}
50+
}
51+
map.put(20, map.getOrDefault(20, 0) + 1);
9152
}
92-
}
9353
}
94-
}
95-
map.put(20, map.getOrDefault(20, 0) + 1);
54+
return true;
9655
}
97-
}
98-
return true;
9956
}
100-
}
10157
}

0 commit comments

Comments
 (0)