1. 121
best-time-to-buy-and-sell-stock/
2. 算法
- dp:O(n)
https://github.com/illuz/leetcode/tree/master/solutions/121.Best_Time_to_Buy_and_Sell_Stock
3. 代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<2)
return 0;
int maxprofile=0;
int curmin=prices[0];
for(int i=1;i<prices.size();i++){
curmin=min(curmin,prices[i]);
maxprofile=max(maxprofile,prices[i]-curmin);
}
return maxprofile;
}
};