8000 refactor 121 · tobeexpert/Leetcode@1f118b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f118b8

Browse files
refactor 121
1 parent 8829017 commit 1f118b8

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@
2424

2525
public class _121 {
2626

27-
/**
28-
* The key here is that you'll have to buy first, before you can sell.
29-
* That means, if the lower price comes after a higher price, their combination won't work! Since you cannot sell first
30-
* before you buy it.
31-
*/
32-
public int maxProfit(int[] prices) {
33-
if (prices == null || prices.length == 0) {
34-
return 0;
35-
}
36-
int buy = prices[0];
37-
int maxProfit = 0;
38-
for (int i = 1; i < prices.length; i++) {
39-
if (prices[i] < buy) {
40-
buy = prices[i];
41-
} else {
42-
maxProfit = Math.max(maxProfit, prices[i] - buy);
27+
public static class Solution1 {
28+
/**
29+
* The key here is that you'll have to buy first, before you can sell. That means, if the lower
30+
* price comes after a higher price, their combination won't wo 8000 rk! Since you cannot sell first
31+
* before you buy it.
32+
*/
33+
public int maxProfit(int[] prices) {
34+
if (prices == null || prices.length == 0) {
35+
return 0;
36+
}
37+
int buy = prices[0];
38+
int maxProfit = 0;
39+
for (int i = 1; i < prices.length; i++) {
40+
if (prices[i] < buy) {
41+
buy = prices[i];
42+
} else {
43+
maxProfit = Math.max(maxProfit, prices[i] - buy);
44+
}
4345
}
46+
return maxProfit;
4447
}
45-
return maxProfit;
4648
}
4749
}

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,35 @@
77
import static junit.framework.Assert.assertEquals;
88

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

0 commit comments

Comments
 (0)
0