class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
// write your code here
if(prices.empty())
return 0;
int maxpro=0;
int minprofit=prices[0];
for(int i=1;i<prices.size();i++)
{
if(prices[i]<minprofit)
minprofit=prices[i];
int money=prices[i]-minprofit;
if(money>maxpro)
maxpro=money;
}
return maxpro;
}
};
原文:http://www.cnblogs.com/muziln/p/6517264.html