Skip to content

Commit 52642b0

Browse files
add a solution for 860
1 parent a9bde29 commit 52642b0

File tree

2 files changed

+68
-35
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+68
-35
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,37 @@ public boolean lemonadeChange(int[] bills) {
5454
return true;
5555
}
5656
}
57+
58+
public static class Solution2 {
59+
/**
60+
* My original solution on 8/14/2024.
61+
* You only need to keep track of the number of $5 and $10 bills at hand.
62+
*/
63+
64+
public boolean lemonadeChange(int[] bills) {
65+
int[] changes = new int[2];//5 and 10
66+
for (int bill : bills) {
67+
if (bill == 5) {
68+
changes[0]++;
69+
} else if (bill == 10) {
70+
if (changes[0] <= 0) {
71+
return false;
72+
} else {
73+
changes[0]--;
74+
}
75+
changes[1]++;
76+
} else if (bill == 20) {
77+
if (changes[1] > 0 && changes[0] > 0) {
78+
changes[1]--;
79+
changes[0]--;
80+
} else if (changes[0] > 2) {
81+
changes[0] -= 3;
82+
} else {
83+
return false;
84+
}
85+
}
86+
}
87+
return true;
88+
}
89+
}
5790
}

src/test/java/com/fishercoder/firstthousand/_860Test.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@
88

99
public class _860Test {
1010

11-
private _860.Solution1 test;
12-
private static int[] bills;
11+
private _860.Solution1 test;
12+
private static int[] bills;
1313

14-
@BeforeEach
14+
@BeforeEach
1515
public 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-
}
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+
}
4848
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy