class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int profit = 0;
int len = prices.size();
if (len < 2) {
return profit;
}
int low = prices[0];
for (int i = 1; i < len; i++) {
low = min(prices[i - 1], low);
profit = max(prices[i] - low, profit);
}
return profit;
}
};
LeetCode Best Time to Buy and Sell Stock
最新推荐文章于 2023-02-28 14:37:52 发布