class Solution {
public:
/**
*
* @param prices int整型vector
* @return int整型
*/
int maxProfit(vector<int>& prices) {
// write code here
//局部最优解
int value = 0;
int min = prices[0];
int minIndex = 0;
int max = prices[0];
int maxIndex = 0;
int i = 0;
while (i<prices.size())
{
if (prices[i]>max)
{
max = prices[i];
maxIndex = i;
}
if (prices[i]<min)
{
min = prices[i];
minIndex = i;
max = 0;
}
if (maxIndex>minIndex)
{
value = value>(max-min)?value:(max-min);
}
i++;
}
return value;
}
};

京公网安备 11010502036488号