点击打开链接
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() <=1) return 0;
int low = prices[0], maxProfit =0;
for(int i=1; i<prices.size(); i++){
int profit = prices[i] - low;
if(maxProfit < profit) maxProfit = profit;
if(prices[i] < low) low = prices[i];
}
return maxProfit;
}
};
![](https://i-blog.csdnimg.cn/blog_migrate/73ee67814d18ab27560d390a4193668c.png)