Skip to content

Commit 3452b68

Browse files
[N-0] refactor 121
1 parent 8c2563f commit 3452b68

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ Your ideas/fixes/algorithms are more than welcome!
522522
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_124.java)| O(n)|O(h) | Hard | Tree, DFS
523523
|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_123.java)| O(?)|O(?) | Hard |
524524
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_122.java)| O(n)|O(1) | Medium | Greedy
525-
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_121.java)| O(n)|O(1) | Easy| DP
525+
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_121.java)| O(n)|O(1) | Easy|
526526
|120|[Triangle](https://leetcode.com/problems/triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_120.java)| O(m*n)|O(n) | Medium| DP
527527
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_119.java)| O(n^2)|O(k) | Easy|
528528
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_118.java)| O(n^2)|O(1) | Easy|

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,18 @@ public class _121 {
3030
* before you buy it.
3131
*/
3232
public int maxProfit(int[] prices) {
33-
if (prices == null || prices.length < 2) {
33+
if (prices == null || prices.length == 0) {
3434
return 0;
3535
}
36-
int minBuy = prices[0];
37-
int maxSell = prices[1];
38-
int maxProfit = (maxSell - minBuy) > 0 ? (maxSell - minBuy) : 0;
36+
int buy = prices[0];
37+
int maxProfit = 0;
3938
for (int i = 1; i < prices.length; i++) {
40-
minBuy = Math.min(minBuy, prices[i]);
41-
maxProfit = Math.max(maxProfit, prices[i] - minBuy);
39+
if (prices[i] < buy) {
40+
buy = prices[i];
41+
} else {
42+
maxProfit = Math.max(maxProfit, prices[i] - buy);
43+
}
4244
}
4345
return maxProfit;
4446
}
45-
46-
public static void main(String... strings) {
47-
// int[] prices = new int[]{7,1,5,3,6,4};
48-
// int[] prices = new int[]{7,6,4,3,1};
49-
// int[] prices = new int[]{2,4,1};
50-
int[] prices = new int[]{1, 2};
51-
_121 test = new _121();
52-
System.out.println(test.maxProfit(prices));
53-
}
5447
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._121;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
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+
}

0 commit comments

Comments
 (0)