思路:
两个变量记录最小价钱和最大盈利, 遍历整个数组, 如果当前这个数大于前面的数, 说明肯定至少有一次增长, 此时求一下最大盈利, 否则的话就更新最小值即可.
int maxProfit(vector<int>& prices) {
if (prices.size() < 2)
return 0;
int minPrice = prices[0], maxProfit = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > prices[i - 1])
maxProfit = max(maxProfit, prices[i] - minPrice);
else
minPrice = min(minPrice, prices[i]);
}
return maxProfit;
}