121. Best Time to Buy and Sell Stock
code:
class Solution {
public:
//[7, 1, 5, 3, 6, 4]
//[5, 4, 3, 2]
int maxProfit(vector<int>& prices) {
if (prices.size() == 0) return 0;
int min = prices[0];
int max = 0;
for (int i = 1; i < prices.size(); i++) {
if (max < prices[i]-min) max = prices[i] - min;
if (min > prices[i]) min = prices[i];
}
return max;
}
};