买卖股票的最佳时机 题解

买卖股票的最佳时机

问题描述

给定一个数组 prices,其中 prices[i] 表示第 i 天的股票价格。你只能选择某一天买入股票,并选择未来的某一天卖出股票,设计一个算法来计算你所能获取的最大利润。

  • 限制条件

    • 只能进行一次交易:也就是说,最多只能买入和卖出各一次。
    • 买入必须在卖出之前:不能在买入之前卖出股票。
  • 目标:返回可以获得的最大利润。如果无法获取任何利润,返回 0

示例
  • 示例 1

    输入:prices = [7,1,5,3,6,4]
    输出:5
    解释:在第 2 天(价格为 1)买入,在第 5 天(价格为 6)卖出,利润为 6 - 1 = 5。
    
  • 示例 2

    输入:prices = [7,6,4,3,1]
    输出:0
    解释:在这种情况下,没有交易完成,所以最大利润为 0。
    
解题思路

为了求最大利润,我们需要在买入价格最低的时候买入,并在之后价格最高的时候卖出。然而,由于我们只能遍历一次数组,并且需要在买入之后才能卖出,因此我们需要一种高效的方法来计算最大利润。

核心思想

  • 维护一个当前为止的最低买入价格 minPrice
  • 计算当前价格与最低买入价格之间的差值,即当前可获得的利润 price - minPrice
  • 维护一个最大利润值 maxProfit,在遍历过程中更新它。
算法步骤
  1. 初始化

    • minPrice = Infinity:表示当前遇到的最低价格,初始为正无穷大。
    • maxProfit = 0:表示当前计算的最大利润,初始为 0。
  2. 遍历价格数组

    对于每个价格 price,执行以下操作:

    • 更新最低价格

      • 如果 price < minPrice,则更新 minPrice = price
    • 计算当前利润并更新最大利润

      • 计算当前利润 profit = price - minPrice
      • 如果 profit > maxProfit,则更新 maxProfit = profit
  3. 返回结果

    • 遍历完成后,maxProfit 即为所能获得的最大利润。
代码详解
function maxProfit(prices: number[]): number {
    let minPrice = Infinity; // 初始化最低价格为无穷大
    let maxProfit = 0;       // 初始化最大利润为 0

    for (let price of prices) {
        if (price < minPrice) {
            minPrice = price; // 更新最低价格
        } else if (price - minPrice > maxProfit) {
            maxProfit = price - minPrice; // 更新最大利润
        }
    }

    return maxProfit;
}
  • 变量初始化

    • minPrice:用于记录当前为止的最低买入价格。
    • maxProfit:用于记录当前为止的最大利润。
  • 遍历数组

    for (let price of prices) {
        ...
    }
    
    • 更新最低价格

      if (price < minPrice) {
          minPrice = price;
      }
      
      • 如果当前价格比之前记录的最低价格还低,更新最低价格。
    • 更新最大利润

      else if (price - minPrice > maxProfit) {
          maxProfit = price - minPrice;
      }
      
      • 如果当前价格与最低价格的差值(利润)大于之前的最大利润,更新最大利润。
  • 返回结果

    return maxProfit;
    
    • 返回计算得到的最大利润。
示例演示

以示例 1 为例,prices = [7,1,5,3,6,4]

  • 初始状态

    • minPrice = Infinity
    • maxProfit = 0
  • 遍历过程

    1. price = 7

      • 7 < Infinity,更新 minPrice = 7
      • 没有更新 maxProfit,因为 price - minPrice = 0
    2. price = 1

      • 1 < 7,更新 minPrice = 1
      • 没有更新 maxProfit,因为 price - minPrice = 0
    3. price = 5

      • 5 > 1,计算利润 5 - 1 = 4
      • 4 > 0,更新 maxProfit = 4
    4. price = 3

      • 3 > 1,计算利润 3 - 1 = 2
      • 2 < 4maxProfit 不变。
    5. price = 6

      • 6 > 1,计算利润 6 - 1 = 5
      • 5 > 4,更新 maxProfit = 5
    6. price = 4

      • 4 > 1,计算利润 4 - 1 = 3
      • 3 < 5maxProfit 不变。
  • 结果

    • 最终 maxProfit = 5
时间和空间复杂度分析
  • 时间复杂度:O(n),其中 n 是数组 prices 的长度。我们只需要遍历一次数组。

  • 空间复杂度:O(1),只使用了常数级别的额外空间。

边界情况处理
  • 价格单调递减

    • 例如 prices = [7,6,4,3,1],在这种情况下,无法获得正利润。
    • 算法会始终更新 minPrice,但 maxProfit 保持为 0。
    • 最终返回 0。
  • 只有一个价格

    • prices.length == 1 时,无法完成交易,利润为 0。
测试代码
function testMaxProfit() {
    const testCases = [
        { prices: [7, 1, 5, 3, 6, 4], expected: 5 },
        { prices: [7, 6, 4, 3, 1], expected: 0 },
        { prices: [1, 2], expected: 1 },
        { prices: [2, 4, 1], expected: 2 },
        { prices: [3], expected: 0 },
    ];

    for (let { prices, expected } of testCases) {
        const result = maxProfit(prices);
        console.assert(result === expected, `测试失败:输入 ${prices},期望输出 ${expected},实际输出 ${result}`);
    }

    console.log("所有测试用例通过!");
}

testMaxProfit();
总结
  • 核心思想:在一次遍历中,找到最低的买入价格和最高的卖出价格(在买入之后)。
  • 算法优势:时间复杂度低,只需要遍历一次数组,空间复杂度为 O(1)。
  • 注意事项:在更新 minPrice 时,只更新更低的价格;在计算利润时,必须确保当前价格是在 minPrice 之后的。
【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李学软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值