Skip to content

Commit 73a6d43

Browse files
committed
Best Time to Buy And Sell Stock
1 parent 8246eb1 commit 73a6d43

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
class Solution {
4+
public int maxProfit(int[] prices) {
5+
int bestProfit = 0;
6+
int buyPrice = prices[0];
7+
for (int i = 1; i < prices.length; i++) {
8+
if (buyPrice > prices[i]) {
9+
buyPrice = prices[i];
10+
continue;
11+
}
12+
bestProfit = Math.max(bestProfit, prices[i] - buyPrice);
13+
}
14+
return bestProfit;
15+
}
16+
}

0 commit comments

Comments
 (0)