121. Best Time to Buy and Sell Stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
int max = 0;
int current = 0;
int next = 0;
if(prices.size() <= 0){
return max;
}
current = prices[0];
for(int i = 1;i < prices.size();i++){
if(current > prices[i]){
current = prices[i];
next = prices[i];
continue;
}
if(next < prices[i]){
next = prices[i];
if(current < prices[i]){
if((prices[i] - current) > max){
max = prices[i] - current;
}
}
}
}
return max;
}
};