返回介绍

Best Time to Buy and Sell Stock

发布于 2025-02-22 13:01:33 字数 2438 浏览 0 评论 0 收藏 0

Source

Say you have an array for
which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction
(ie, buy one and sell one share of the stock),
design an algorithm to find the maximum profit.

Example
Given an example [3,2,3,1,2], return 1

题解

最多只允许进行一次交易,显然我们只需要把波谷和波峰分别找出来就好了。但是这样的话问题又来了,有多个波峰和波谷时怎么办?——找出差值最大的一对波谷和波峰。故需要引入一个索引用于记录当前的波谷,结果即为当前索引值减去波谷的值。

Python

class Solution:
  """
  @param prices: Given an integer array
  @return: Maximum profit
  """
  def maxProfit(self, prices):
    if prices is None or len(prices) <= 1:
      return 0

    profit = 0
    cur_price_min = 2**31 - 1
    for price in prices:
      profit = max(profit, price - cur_price_min)
      cur_price_min = min(cur_price_min, price)

    return profit

C++

class Solution {
public:
  /**
   * @param prices: Given an integer array
   * @return: Maximum profit
   */
  int maxProfit(vector<int> &prices) {
    if (prices.size() <= 1) return 0;

    int profit = 0;
    int cur_price_min = INT_MAX;
    for (int i = 0; i < prices.size(); ++i) {
      profit = max(profit, prices[i] - cur_price_min);
      cur_price_min = min(cur_price_min, prices[i]);
    }

    return profit;
  }
};

Java

public class Solution {
  /**
   * @param prices: Given an integer array
   * @return: Maximum profit
   */
  public int maxProfit(int[] prices) {
    if (prices == null || prices.length <= 1) return 0;

    int profit = 0;
    int curPriceMin = Integer.MAX_VALUE;
    for (int price : prices) {
      profit = Math.max(profit, price - curPriceMin);
      curPriceMin = Math.min(curPriceMin, price);
    }

    return profit;
  }
}

源码分析

善用 maxmin 函数,减少 if 的使用。

复杂度分析

遍历一次 prices 数组,时间复杂度为 O(n)O(n)O(n), 使用了几个额外变量,空间复杂度为 O(1)O(1)O(1).

Reference

  • soulmachine 的卖股票系列

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文