(一)题目描述
(二)思想方法
遍历数组,如果有小的就从后面找大的减去。
(三)代码实现
class Solution {
public:
int maxProfit(vector<int>& prices) {
int pricesSize=prices.size();
if(pricesSize<=0)
return 0;
int i;
int price=prices[0];
int profit=0;
for(i=1;i<pricesSize;i++)
{
if(prices[i]<price)
price=prices[i];
if(profit<prices[i]-price)
profit=prices[i]-price;
}
return profit;
}
};