暴力破解法
递归算法
动态规划
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,121,最佳时机,prices,nMinPrice,LeetCode,size
From: https://www.cnblogs.com/ganshang/p/17016107.html