class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<1) return 0;
int lowprice = prices[0], maxProfit = 0;
for(int i=1; i<prices.size(); i++){
int profit = prices[i]-lowprice;
maxProfit = max(maxProfit, profit);
lowprice = min(lowprice, prices[i]);
}
return maxProfit;
}
};
leetcode121+Best Time to Buy and Sell Stock+dp
最新推荐文章于 2024-11-02 15:25:13 发布