Skip to content

Commit 3d3127d

Browse files
author
Li Li
committed
add code of 121
1 parent c5fcd3a commit 3d3127d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// mantain two value: minPrice, maxProfit
2+
public class Solution {
3+
public int MaxProfit(int[] prices) {
4+
if (prices == null || prices.Length < 2) {
5+
return 0;
6+
}
7+
int minPrice = prices[0];
8+
int maxProfit = 0;
9+
for (int i = 1; i < prices.Length; i++) {
10+
if (prices[i] < minPrice) {
11+
minPrice = prices[i];
12+
} else if (prices[i] - minPrice > maxProfit) {
13+
maxProfit = prices[i] - minPrice;
14+
}
15+
}
16+
return maxProfit;
17+
}
18+
}

0 commit comments

Comments
 (0)