LeetCode 买卖股票的最佳时机

141 阅读2分钟

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0

示例 1:

输入:

[7,1,5,3,6,4]

输出:

5

解释:

在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

示例 2:

输入:

prices = [7,6,4,3,1]

输出:

0

解释:

在这种情况下, 没有交易完成, 所以最大利润为 0。

提示:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104

代码:

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

思路:

找出给定数组中两个数字之间的最大差值(即,最大利润)。此外,第二个数字(卖出价格)必须大于第一个数字(买入价格)。

形式上,对于每组 i 和 j(其中 j > i )我们需要找出max(prices[j]−prices[i])。

复杂度分析:

  • 时间复杂度:O(n2)O(n^2)。循环运行n(n1)2\frac{n(n−1)}{2}次。
  • 空间复杂度:O(1)。只使用了常数个变量。

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n == 0) return 0; // 边界条件
        int minprice = prices[0];
        vector<int> dp (n, 0);

        for (int i = 1; i < n; i++){
            minprice = min(minprice, prices[i]);
            dp[i] = max(dp[i - 1], prices[i] - minprice);
        }
        return dp[n - 1];
    }
};

思路:

动态规划一般分为一维、二维、多维(使用状态压缩),对应形式为 dp(i)、dp(i)(j)、二进制dp(i)(j)。

  1. 动态规划做题步骤

明确 dp(i) 应该表示什么(二维情况:dp(i)(j)); 根据 dp(i) 和 dp(i−1) 的关系得出状态转移方程; 确定初始条件,如 dp(0)。 2. 本题思路

其实官方代码的思路不是凭空想象的,而是由动态规划的思想演变而来。这里介绍一维动态规划思想。

dp[i] 表示前 i 天的最大利润,因为我们始终要使利润最大化,则:

dp[i] = max(dp[i-1], prices[i]-minprice) dp[i]=max(dp[i−1],prices[i]−minprice)

复杂度分析:

  • 时间复杂度:O(n)。
  • 空间复杂度:O(n)。

官方代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int inf = 1e9;
        int minprice = inf, maxprofit = 0;
        for (int price: prices) {
            maxprofit = max(maxprofit, price - minprice);
            minprice = min(price, minprice);
        }
        return maxprofit;
    }
};

思路:

假设给定的数组为:[7, 1, 5, 3, 6, 4]

如果我们在图表上绘制给定数组中的数字,我们将会得到:

我们来假设自己来购买股票。随着时间的推移,每天我们都可以选择出售股票与否。那么,假设在第 i 天,如果我们要在今天卖股票,那么我们能赚多少钱呢?

显然,如果我们真的在买卖股票,我们肯定会想:如果我是在历史最低点买的股票就好了!太好了,在题目中,我们只要用一个变量记录一个历史最低价格 minprice,我们就可以假设自己的股票是在那天买的。那么我们在第 i 天卖出股票能得到的利润就是 prices[i] - minprice

因此,我们只需要遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:如果我是在历史最低点买进的,那么我今天卖出能赚多少钱?当考虑完所有天数之时,我们就得到了最好的答案。

复杂度分析:

  • 时间复杂度:O(n),只需要遍历一次。
  • 空间复杂度:O(1),只使用了常数个变量。