【题解】121. 买卖股票的最佳时机(数组、动态规划)

https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150
在这里插入图片描述

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<int> dp(n);
        dp[0] = 0;
        int m = prices[0];  // 存前面遍历过的最小值
        for (int i = 1; i < n; ++i)
        {
            dp[i] = max(dp[i-1], prices[i] - m);
            m = min(m, prices[i]);
        }
        return dp[n-1];
    }
};

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 16:04:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 16:04:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 16:04:03       58 阅读
  4. Python语言-面向对象

    2024-07-19 16:04:03       69 阅读

热门阅读

  1. Linux常用命令(持续更新)

    2024-07-19 16:04:03       19 阅读
  2. spring boot 实现token验证登陆状态

    2024-07-19 16:04:03       21 阅读
  3. nginx的安装和使用

    2024-07-19 16:04:03       21 阅读
  4. 深入了解 GCC

    2024-07-19 16:04:03       20 阅读
  5. 【MyBatis】Mybatis中的动态SQL——bind标签

    2024-07-19 16:04:03       20 阅读
  6. GreenDao实现原理

    2024-07-19 16:04:03       20 阅读
  7. 分布式缓存设计:深入理解 Memcached 架构

    2024-07-19 16:04:03       21 阅读
  8. 项目相关方不配合,项目经理怎么办?

    2024-07-19 16:04:03       21 阅读
  9. oneos虚拟文件系统vfs源码分析

    2024-07-19 16:04:03       17 阅读
  10. 富格林:正规手段识破欺诈套路

    2024-07-19 16:04:03       18 阅读
  11. Redis生产问题

    2024-07-19 16:04:03       19 阅读
  12. Makefile: 解决.c文件依赖.h文件的问题

    2024-07-19 16:04:03       17 阅读