Leetcode #121. Best Time to Buy and Sell Stock
题目
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty()) return 0;
int curmin = prices[0], curmax = -1;
for (auto p : prices) {
if (p < curmin) {
curmin = p;
continue;
} else {
if (curmax < p - curmin) curmax = p - curmin;
}
}
return curmax;
}
};