Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit52642b0

Browse files
add a solution for 860
1 parenta9bde29 commit52642b0

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
returntrue;
5555
}
5656
}
57+
58+
publicstaticclassSolution2 {
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+
publicbooleanlemonadeChange(int[]bills) {
65+
int[]changes =newint[2];//5 and 10
66+
for (intbill :bills) {
67+
if (bill ==5) {
68+
changes[0]++;
69+
}elseif (bill ==10) {
70+
if (changes[0] <=0) {
71+
returnfalse;
72+
}else {
73+
changes[0]--;
74+
}
75+
changes[1]++;
76+
}elseif (bill ==20) {
77+
if (changes[1] >0 &&changes[0] >0) {
78+
changes[1]--;
79+
changes[0]--;
80+
}elseif (changes[0] >2) {
81+
changes[0] -=3;
82+
}else {
83+
returnfalse;
84+
}
85+
}
86+
}
87+
returntrue;
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
publicclass_860Test {
1010

11-
private_860.Solution1test;
12-
privatestaticint[]bills;
11+
private_860.Solution1test;
12+
privatestaticint[]bills;
1313

14-
@BeforeEach
14+
@BeforeEach
1515
publicvoidsetUp() {
16-
test =new_860.Solution1();
17-
}
18-
19-
@Test
20-
publicvoidtest1() {
21-
bills =newint[]{5,5,5,10,20};
22-
assertEquals(true,test.lemonadeChange(bills));
23-
}
24-
25-
@Test
26-
publicvoidtest2() {
27-
bills =newint[]{5,5,10};
28-
assertEquals(true,test.lemonadeChange(bills));
29-
}
30-
31-
@Test
32-
publicvoidtest3() {
33-
bills =newint[]{10,10};
34-
assertEquals(false,test.lemonadeChange(bills));
35-
}
36-
37-
@Test
38-
publicvoidtest4() {
39-
bills =newint[]{5,5,10,10,20};
40-
assertEquals(false,test.lemonadeChange(bills));
41-
}
42-
43-
@Test
44-
publicvoidtest5() {
45-
bills =newint[]{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+
publicvoidtest1() {
21+
bills =newint[]{5,5,5,10,20};
22+
assertEquals(true,test.lemonadeChange(bills));
23+
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
bills =newint[]{5,5,10};
28+
assertEquals(true,test.lemonadeChange(bills));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
bills =newint[]{10,10};
34+
assertEquals(false,test.lemonadeChange(bills));
35+
}
36+
37+
@Test
38+
publicvoidtest4() {
39+
bills =newint[]{5,5,10,10,20};
40+
assertEquals(false,test.lemonadeChange(bills));
41+
}
42+
43+
@Test
44+
publicvoidtest5() {
45+
bills =newint[]{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)

[8]ページ先頭

©2009-2025 Movatter.jp