Skip to content

Commit 9aaaeab

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 121_Best_Time_to_Buy_and_Sell_Stock.java
1 parent a072c04 commit 9aaaeab

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
if (prices == null || prices.length == 0) {
4+
return 0;
5+
}
6+
7+
int minPrices = prices[0], profit = 0;
8+
9+
for (int i = 1; i < prices.length; i++) {
10+
if (prices[i] < minPrices) {
11+
minPrices = Math.min(minPrices, prices[i]);
12+
}
13+
14+
profit = Math.max(profit, prices[i] - minPrices);
15+
}
16+
17+
return profit;
18+
}
19+
}

0 commit comments

Comments
 (0)