代码随想录算法训练营第四十九天| 121 买卖股票的最佳时机 122 买卖股票的最佳时机 ||

目录

121 买卖股票的最佳时机

122 买卖股票的最佳时机 ||


121 买卖股票的最佳时机

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        int now = prices[0];
        for(int i = 1;i < prices.size();i++){
            if(prices[i] > now){
                res = max(res,prices[i] - now);
            }
            now = min(now,prices[i]);
        }
        return res;
    }
};

时间复杂度O(n)

空间复杂度O(1)

122 买卖股票的最佳时机 ||

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        int now = prices[0];
        for(int i = 1;i < prices.size();i++){
            if(prices[i] > now)res += prices[i] - now;
            now = prices[i];
        }
        return res;
    }
};

时间复杂度O(n)

空间复杂度O(1)

相关推荐

最近更新

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

    2023-12-10 18:02:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 18:02:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 18:02:02       82 阅读
  4. Python语言-面向对象

    2023-12-10 18:02:02       91 阅读

热门阅读

  1. 深入了解linux下网卡防火墙selinux

    2023-12-10 18:02:02       63 阅读
  2. 力扣:200. 岛屿数量(Python3)

    2023-12-10 18:02:02       64 阅读
  3. CentOS 7 离线安装Docker

    2023-12-10 18:02:02       44 阅读
  4. 【Rust】第二节:入门(如入)

    2023-12-10 18:02:02       64 阅读
  5. 力扣面试150题 | 多数元素

    2023-12-10 18:02:02       48 阅读
  6. 【 六袆 - Framework】ActiveMQ in windows安装;

    2023-12-10 18:02:02       54 阅读
  7. Linux 基本了解

    2023-12-10 18:02:02       49 阅读
  8. NVMe over Fabrics with SPDK with iRDMA总结 - 2

    2023-12-10 18:02:02       40 阅读
  9. 说说设计体系、风格指南和模式库

    2023-12-10 18:02:02       52 阅读