动态优化买股票
int maxProfit(vector<int>& prices) {
if (prices.size() == 0)
{
return 0;
}
int min = prices[0];
int result = 0;
for (int i = 1; i < prices.size(); ++i)
{
if (result < prices[i] - min)
{
result = prices[i] - min;
}
if (prices[i] < min)
{
min = prices[i];
}
}
return result;
}
int maxProfit(vector<int>& prices) {
int tmp,start=prices[0],retmax = 0;
for(int i=1;i<prices.size();++i)
{
if(prices[i]>prices[i-1]){
tmp = prices[i] - start ;
}else{
start = prices[i]>start?start:prices[i];
}
retmax = tmp>retmax?tmp:retmax;
}
return retmax;
}