Best Time to Buy and Sell Stock
這是一道easy題,考察Dynamic programming暂刘。因?yàn)閯偞蜷_leetcode 309, Best Time to Buy and Sell Stock with Cooldown, 所以就把leetcode121這道題拿出來(lái)復(fù)習(xí)了一下
class Solution {
public:
int maxProfit(vector<int>& prices) {
int max_profit = 0;
int min_price = INT_MAX;
for (auto p: prices) {
min_price = min(min_price, p);
max_profit = max(max_profit, p - min_price);
}
return max_profit;
}
};