Skip to content

Commit c86130a

Browse files
committed
solved Best Time to Buy and Sell Stock
1 parent aa47fcc commit c86130a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
3+
class Solution {
4+
public:
5+
int maxProfit(vector<int>& prices) {
6+
int n = prices.size();
7+
int minimum = prices[0];
8+
int ans = 0;
9+
for(int i=1; i<n; i++) {
10+
ans = max(ans, prices[i] - minimum);
11+
minimum = min(minimum, prices[i]);
12+
}
13+
return ans;
14+
}
15+
};

0 commit comments

Comments
 (0)