LeetCode题解之股票系列问题

对于力扣平台上的股票类型的题目:

121. 买卖股票的最佳时机

解题思路

首先找到当天之前股票价格的最小值,然后遍历prices数组,找到当前股票价钱与最小值的差作为最大的利润值。

代码

单调栈思想
class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int minPrice = Integer.MAX_VALUE;  // 当天之前的最小值
        int maxVal = 0; // 最大收益值
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            }
            if (prices[i] - minPrice > maxVal) {
                maxVal = prices[i] - minPrice;
            }
        }
        return maxVal;
    }
}

122. 买卖股票的最佳时机 II

代码

单调栈思想
class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        int maxVal = 0; // 最大收益值
        for (int i = 1; i < n; i++) {
            // 如果第i-1天股票的价钱比第i天少,则在第i-1天买入股票,在第i天卖出股票
            // 这样可以保证你获得的利润是整个周期中最大的(所有的可以赚钱的时机都被你占了)
            int prifit = prices[i] - prices[i - 1];
            if (prifit > 0) {
                maxVal += prifit;
            }
        }
        return maxVal;
    }
}
动态规划V1
class Solution {
    public int maxProfit(int[] prices) {
        // dp[i][0] 表示第i天结束后持有股票获得的最大收益值  => 1. 当天买入股票; 2. 之前持有股票没有卖
        // dp[i][1] 表示第i天结束后不持有股票获得的最大收益值  => 1. 当天卖出股票; 2. 之前就没有持有股票

        // dp[i][0] = max(dp[i - 1][0] - prices[i], dp[i - 1][1])
        // dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])

        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = -prices[0];
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][1] - prices[i], dp[i - 1][0]);
            dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
        }
        return Math.max(dp[n - 1][0], dp[n - 1][1]);
    }
}
动态规划V2
class Solution {
    public int maxProfit(int[] prices) {
        // dp[i][0] 表示第i天结束后持有股票获得的最大收益值  => 1. 当天买入股票; 2. 之前持有股票没有卖
        // dp[i][1] 表示第i天结束后不持有股票获得的最大收益值  => 1. 当天卖出股票; 2. 之前就没有持有股票

        // dp[i][0] = max(dp[i - 1][0] - prices[i], dp[i - 1][1])
        // dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])

        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        int[][] dp = new int[n][2];
        int s0 = -prices[0];
        int s1 = 0;
        for (int i = 1; i < n; i++) {
            int news0 = Math.max(s1 - prices[i], s0);
            int news1 = Math.max(s0 + prices[i], s1);
            s0 = news0;
            s1 = news1;
        }
        return Math.max(s0, s1);
    }
}

123. 买卖股票的最佳时机 III

代码

动态规划
class Solution {
    public int maxProfit(int[] prices) {
        // dp[i][m][k]  表示第i天结束后,m表示当前是否持有股票,已经完成的交易次数k
        // i的取值为0到n-1
        // m的取值为 0 或 1
        // k的取值为0, 1, 2
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        int[][][] dp = new int[n][2][3];

        // 初始化dp数组
        dp[0][0][0] = dp[0][0][1] = dp[0][0][2] = 0;
        dp[0][1][0] = dp[0][1][1] = dp[0][1][2] = -prices[0];

        for (int i = 1; i < n; i++) {
            dp[i][0][0] = 0;
            dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][1][0] + prices[i]);  // 完成交易次数1 => 当天卖出股票或者之前就已经交易了一次,当天未做任何交易
            dp[i][0][2] = Math.max(dp[i - 1][0][2], dp[i - 1][1][1] + prices[i]);
            dp[i][1][0] = Math.max(dp[i - 1][1][0], dp[i - 1][0][0] - prices[i]);  // 只有卖出股票才算一次交易
            dp[i][1][1] = Math.max(dp[i - 1][1][1], dp[i - 1][0][1] - prices[i]);
            dp[i][1][2] = 0;
        }

        return Math.max(dp[n - 1][0][1], dp[n - 1][0][2]);
    }
}

188. 买卖股票的最佳时机 IV

解题思路

刚开始使用传统的dp方式去做,在代码写出来之后就内存超出了,所以放弃了传统dp的方式。之后查看题解,发现是使用的另外一种方法。

首先需要考虑k和prices数组长度之间的关系:

  1. k=0的时候,即表示不进行交易,直接返回0即可。
  2. k < prices.length / 2的时候,表示可以进行无限次交易(每次交易都需要两天)

所以当 k < prices.length / 2的时候,就可以直接按照无限交易次数来进行了。

接下来需要处理一般情况:

  1. 定义一个dp数组,存储了进行k次交易且买入或者卖出股票时候的状态
  2. 处理base case(交易次数为0的情况)
  3. 处理一般情况(模拟买入和卖出股票)

代码

class Solution {
    public int maxProfit(int k, int[] prices) {
        if (prices == null || prices.length == 0 || k == 0) {
            return 0;
        }
        int n = prices.length;
        // dp[i][j][m] 表示第i天结束后,持有股票为状态j并且还能执行的交易数为m的时候的最大利润
        // i 的取值为 [0, n)
        // j 的取值为 [0, 1]  => 其中0表示不持有股票,1表示持有股票
        // km的取值为 [0, k + 1]
        // dp[i][0][k] = max(dp[i - 1][0][k], dp[i - 1][1][k - 1] + prices[i])  => 1. 前一天没有股票,并且今天没有进行交易 2. 今天将股票卖出
        // dp[i][1][k] = max(dp[i - 1][1][k], dp[i - 1][0][k] - prices[i])  => 1. 前一天没有股票,并且今天没有进行交易  2. 今天将股票买入
        // 经过以上分析写出来的动态规划代码存在内存超出限制的问题
        if (k > n / 2) {
            return maxProfitInfinity(prices);
        }

        // k表示交易的次数
        // 0 表示买入股票
        // 1 表示卖出股票
        int[][] dp = new int[k][2];
        for (int i = 0; i < k; i++) {
            dp[i][0] = Integer.MIN_VALUE;
        }

        for (int p : prices) {
        	// 单独处理交易次数为 0 的情况
            dp[0][0] = Math.max(dp[0][0], 0 - p);
            // 交易一次含有股票说明在交易0次的时候含有股票,并且这次交易将其卖出
            dp[0][1] = Math.max(dp[0][1], dp[0][0] + p);

            for (int i = 1; i < k; i++) {
                dp[i][0] = Math.max(dp[i][0], dp[i - 1][1] - p);  // 买入股票
                dp[i][1] = Math.max(dp[i][1], dp[i][0] + p);  // 卖出股票
            }
        }
        return dp[k - 1][1];
    }

    private int maxProfitInfinity(int[] prices) {
        int res = 0;
        for (int i = 1; i < prices.length; i++) {
            int profit = prices[i] - prices[i - 1];
            if (profit > 0) {
                res += profit;
            }
        }
        return res;
    }
}

309. 最佳买卖股票时期含冷冻期

LeetCode原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

代码

动态规划V1
class Solution {
    public int maxProfit(int[] prices) {
        // dp[i][0]   第i天结束后且持有股票累积的最大收益值
        // dp[i][1]   第i天结束后不持有股票且未进入冷冻期的最大收益
        // dp[i][2]   第i天结束后不持有股票且进入冷冻期的最大收益
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        int[][] dp = new int[n][3];
        dp[0][0] = - prices[0];
        for(int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
            dp[i][1] = Math.max(dp[i - 1][2], dp[i - 1][1]);
            dp[i][2] = dp[i - 1][0] + prices[i];
        }
        return Math.max(dp[n - 1][1], dp[n - 1][2]);
    }
}

复杂度分析:

  • 时间复杂度:遍历数组一次,时间复杂度为 O ( n ) O(n) O(n)
  • 空间复杂度:使用了长度为n的数组存储dp的结果,所以空间复杂度为 O ( n ) O(n) O(n)

观察dp的过程可以发现,当天的状态只与前一天有关,所以可以只用几个变量来存储前一天的状态,将空间复杂度优化为 O ( 1 ) O(1) O(1)

动态规划V2
class Solution {
    public int maxProfit(int[] prices) {
        // dp[i][0]   第i天结束后且持有股票累积的最大收益值
        // dp[i][1]   第i天结束后不持有股票且未进入冷冻期的最大收益
        // dp[i][2]   第i天结束后不持有股票且进入冷冻期的最大收益
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        int[][] dp = new int[n][3];
        int s0 = - prices[0];
        int s1 = 0;
        int s2 = 0;
        for(int i = 1; i < n; i++) {
            int news0 = Math.max(s0, s1 - prices[i]);
            int news1 = Math.max(s1, s2);
            int news2 = s0 + prices[i];
            s0 = news0;
            s1 = news1;
            s2 = news2;
        }
        return Math.max(s1, s2);
    }
}
  • 时间复杂度:由于只遍历了prices数组一次,所以时间复杂度为 O ( n ) O(n) O(n)
  • 空间复杂度:循环中只使用了三个变量来存储上一天的状态,使用了常数级的空间,所以空间复杂度为 O ( 1 ) O(1) O(1)

714. 买卖股票的最佳时机含手续费

代码

class Solution {
    public int maxProfit(int[] prices, int fee) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int n = prices.length;
        // dp[i][m] 表示第i天结束后状态m的最大利润
        // i 的取值范围为 [0, n)
        // m 的取值为0或者1=,为0时表示不持有股票,为1时候表示持有股票
        int[][] dp = new int[n][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[n - 1][0];
    }
}

剑指 Offer 63. 股票的最大利润

与 第一题 121. 买卖股票的最佳时机 一致。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值