class Solution {
public:
int maxProfit(vector<int>& prices) {
int lowest_price = prices[0];
int profit = 0;
for (int i = 0; i < prices.size(); i++) {
if (profit < prices[i] - lowest_price) {
profit = prices[i] - lowest_price;
}
if (prices[i] < lowest_price) {
lowest_price = prices[i];
}
}
return profit;
}
};
思路
找价格最低点,然后求之后每天卖出的利润;如果有更低的价格,则更新最低价,继续求之后每天卖出的利润,找利润最大值;
要点
无
感觉像是动态规划的思想