Skip to content

Commit 96a1ff0

Browse files
refactor 121
1 parent 85ab653 commit 96a1ff0

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
public class _121 {
44

55
public static class Solution1 {
6+
/**
7+
* My very original but not super optimal solution.
8+
*/
9+
public int maxProfit(int[] prices) {
10+
int[] maxPriceAfterThisDay = new int[prices.length];
11+
int maxSell;
12+
for (int i = prices.length - 1; i > 0; i--) {
13+
maxSell = Math.max(maxPriceAfterThisDay[i], prices[i]);
14+
maxPriceAfterThisDay[i] = maxSell;
15+
}
16+
int[] minPriceBeforeThisDay = new int[prices.length];
17+
int minBuy = prices[0];
18+
for (int i = 0; i < prices.length - 1; i++) {
19+
minBuy = Math.min(prices[i], minBuy);
20+
minPriceBeforeThisDay[i] = minBuy;
21+
}
22+
int maxPro = 0;
23+
for (int i = 0; i < prices.length - 1; i++) {
24+
maxPro = Math.max(maxPro, maxPriceAfterThisDay[i + 1] - minPriceBeforeThisDay[i]);
25+
}
26+
return maxPro;
27+
}
28+
}
29+
30+
public static class Solution2 {
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

+37-31
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,41 @@
77
import static junit.framework.Assert.assertEquals;
88

99
public class _121Test {
10-
private static _121.Solution1 solution1;
11-
private static int[] prices;
12-
13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _121.Solution1();
16-
}
17-
18-
@Test
19-
public void test1() {
20-
prices = new int[] {7, 1, 5, 3, 6, 4};
21-
assertEquals(5, solution1.maxProfit(prices));
22-
}
23-
24-
@Test
25-
public void test2() {
26-
prices = new int[] {7, 6, 4, 3, 1};
27-
assertEquals(0, solution1.maxProfit(prices));
28-
}
29-
30-
@Test
31-
public void test3() {
32-
prices = new int[] {2, 4, 1};
33-
assertEquals(2, solution1.maxProfit(prices));
34-
}
35-
36-
@Test
37-
public void test4() {
38-
prices = new int[] {1, 2};
39-
assertEquals(1, solution1.maxProfit(prices));
40-
}
10+
private static _121.Solution1 solution1;
11+
private static _121.Solution2 solution2;
12+
private static int[] prices;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _121.Solution1();
17+
solution2 = new _121.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
prices = new int[]{7, 1, 5, 3, 6, 4};
23+
assertEquals(5, solution1.maxProfit(prices));
24+
assertEquals(5, solution2.maxProfit(prices));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
prices = new int[]{7, 6, 4, 3, 1};
30+
assertEquals(0, solution1.maxProfit(prices));
31+
assertEquals(0, solution2.maxProfit(prices));
32+
}
33+
34+
@Test
35+
public void test3() {
36+
prices = new int[]{2, 4, 1};
37+
assertEquals(2, solution1.maxProfit(prices));
38+
assertEquals(2, solution2.maxProfit(prices));
39+
}
40+
41+
@Test
42+
public void test4() {
43+
prices = new int[]{1, 2};
44+
assertEquals(1, solution1.maxProfit(prices));
45+
assertEquals(1, solution2.maxProfit(prices));
46+
}
4147
}

0 commit comments

Comments
 (0)