Skip to content

Commit 3af456c

Browse files
committed
solve best ime to buy and sell stock
1 parent 0671d92 commit 3af456c

File tree

1 file changed

+17
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)