🔗 https://leetcode.cn/problems/best-time-to-buy-and-sell-stock
题目
- nums 表示连续几天的股票价格,返回最大利润
思路
- 贪心,模拟
代码
class Solution {
public:int maxProfit(vector<int>& prices) {int ans = 0;int min_price = prices[0];for (int i = 1; i < prices.size(); i++) {if (prices[i] > min_price) {ans = max(ans, prices[i] - min_price);} else {min_price = prices[i];}}return ans;}
};