Skip to content

Commit c16dafc

Browse files
committed
feat: best-time-to-buy-and-sell-stock solution
1 parent cf65e7b commit c16dafc

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int maxProfit = 0;
4+
int minPrice = prices[0];
5+
6+
for (int i = 0; i < prices.length; i++) {
7+
int profit = prices[i] - minPrice;
8+
maxProfit = Math.max(maxProfit, profit);
9+
minPrice = Math.min(minPrice, prices[i]);
10+
}
11+
return maxProfit;
12+
}
13+
}

0 commit comments

Comments
 (0)