Skip to content

Commit ff01d9f

Browse files
authored
2022-07-09 update: added "Lemonade Change" (#28)
1 parent 2a59c32 commit ff01d9f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
// https://leetcode.com/problems/lemonade-change/
4+
public class LemonadeChange {
5+
6+
private final int[] input;
7+
8+
public LemonadeChange(int[] input) {
9+
this.input = input;
10+
}
11+
12+
public boolean solution() {
13+
int five = 0;
14+
int ten = 0;
15+
for (int bill : input) {
16+
if (bill == 5) {
17+
five++;
18+
} else if (bill == 10) {
19+
if (five == 0) {
20+
return false;
21+
}
22+
five--;
23+
ten++;
24+
} else {
25+
if (five > 0 && ten > 0) {
26+
five--;
27+
ten--;
28+
} else if (five >= 3) {
29+
five -= 3;
30+
} else {
31+
return false;
32+
}
33+
}
34+
}
35+
return true;
36+
}
37+
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class LemonadeChangeTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertTrue(new LemonadeChange(new int[]{5, 5, 5, 10, 20}).solution());
12+
}
13+
14+
}

0 commit comments

Comments
 (0)