Skip to content

Commit d7e3162

Browse files
authored
Add solution for Best Time to Buy and Sell Stock
Implemented a solution to calculate maximum profit from stock prices.
1 parent 10a0de1 commit d7e3162

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2. Best Time to Buy and Sell Stock (Easy)
2+
3+
Problem: Max profit from one buy-sell transaction.
4+
class Solution {
5+
public int maxProfit(int[] prices) {
6+
int min = prices[0], profit = 0;
7+
for (int p : prices) {
8+
min = Math.min(min, p);
9+
profit = Math.max(profit, p - min);
10+
}
11+
return profit;
12+
}
13+
}

0 commit comments

Comments
 (0)