class Solution {
public:
int maxProfit(vector<int>& prices) {
size_t nPriceSize = prices.size();
int nMaxProfit = 0;
if (nPriceSize > 1)
{
int nMinPrice = prices[0];
for (size_t i = 1; i < nPriceSize; i++) // 从1开始即可,小于1时最大利润还是0
{
nMaxProfit = std::max<int>(nMaxProfit, prices[i] - nMinPrice);
nMinPrice = std::min<int>(nMinPrice, prices[i]);
}
}
return nMaxProfit;
}
};
标签:nPriceSize,int,股票,nMaxProfit,prices,size,nMinPrice,LeetCode,利润
From: https://www.cnblogs.com/ganshang/p/17016043.html