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

Commit96a1ff0

Browse files
refactor 121
1 parent85ab653 commit96a1ff0

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

‎src/main/java/com/fishercoder/solutions/_121.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
publicclass_121 {
44

55
publicstaticclassSolution1 {
6+
/**
7+
* My very original but not super optimal solution.
8+
*/
9+
publicintmaxProfit(int[]prices) {
10+
int[]maxPriceAfterThisDay =newint[prices.length];
11+
intmaxSell;
12+
for (inti =prices.length -1;i >0;i--) {
13+
maxSell =Math.max(maxPriceAfterThisDay[i],prices[i]);
14+
maxPriceAfterThisDay[i] =maxSell;
15+
}
16+
int[]minPriceBeforeThisDay =newint[prices.length];
17+
intminBuy =prices[0];
18+
for (inti =0;i <prices.length -1;i++) {
19+
minBuy =Math.min(prices[i],minBuy);
20+
minPriceBeforeThisDay[i] =minBuy;
21+
}
22+
intmaxPro =0;
23+
for (inti =0;i <prices.length -1;i++) {
24+
maxPro =Math.max(maxPro,maxPriceAfterThisDay[i +1] -minPriceBeforeThisDay[i]);
25+
}
26+
returnmaxPro;
27+
}
28+
}
29+
30+
publicstaticclassSolution2 {
631
/**
732
* The key here is that you'll have to buy first, before you can sell. That means, if the lower
833
* price comes after a higher price, their combination won't work! Since you cannot sell first

‎src/test/java/com/fishercoder/_121Test.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,41 @@
77
importstaticjunit.framework.Assert.assertEquals;
88

99
publicclass_121Test {
10-
privatestatic_121.Solution1solution1;
11-
privatestaticint[]prices;
12-
13-
@BeforeClass
14-
publicstaticvoidsetup() {
15-
solution1 =new_121.Solution1();
16-
}
17-
18-
@Test
19-
publicvoidtest1() {
20-
prices =newint[] {7,1,5,3,6,4};
21-
assertEquals(5,solution1.maxProfit(prices));
22-
}
23-
24-
@Test
25-
publicvoidtest2() {
26-
prices =newint[] {7,6,4,3,1};
27-
assertEquals(0,solution1.maxProfit(prices));
28-
}
29-
30-
@Test
31-
publicvoidtest3() {
32-
prices =newint[] {2,4,1};
33-
assertEquals(2,solution1.maxProfit(prices));
34-
}
35-
36-
@Test
37-
publicvoidtest4() {
38-
prices =newint[] {1,2};
39-
assertEquals(1,solution1.maxProfit(prices));
40-
}
10+
privatestatic_121.Solution1solution1;
11+
privatestatic_121.Solution2solution2;
12+
privatestaticint[]prices;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_121.Solution1();
17+
solution2 =new_121.Solution2();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
22+
prices =newint[]{7,1,5,3,6,4};
23+
assertEquals(5,solution1.maxProfit(prices));
24+
assertEquals(5,solution2.maxProfit(prices));
25+
}
26+
27+
@Test
28+
publicvoidtest2() {
29+
prices =newint[]{7,6,4,3,1};
30+
assertEquals(0,solution1.maxProfit(prices));
31+
assertEquals(0,solution2.maxProfit(prices));
32+
}
33+
34+
@Test
35+
publicvoidtest3() {
36+
prices =newint[]{2,4,1};
37+
assertEquals(2,solution1.maxProfit(prices));
38+
assertEquals(2,solution2.maxProfit(prices));
39+
}
40+
41+
@Test
42+
publicvoidtest4() {
43+
prices =newint[]{1,2};
44+
assertEquals(1,solution1.maxProfit(prices));
45+
assertEquals(1,solution2.maxProfit(prices));
46+
}
4147
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp