题意:给出股票价格,求出买入卖出后的最大收益。
思路:贪心。找到每一天前的最低股价。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0;
int minprice = 999999;
for(int i = 0; i < prices.size(); ++ i) {
if(profit < prices[i] - minprice) profit = prices[i] - minprice;
if(minprice > prices[i]) minprice = prices[i];
}
return profit;
}
};