LeetCode股票问题总结

包含了,
1.题目121. 买卖股票的最佳时机,(买卖一次)
2.题目122. 买卖股票的最佳时机 II(无限次购买)
3.题目714. 买卖股票的最佳时机含手续费
4.题目309. 最佳买卖股票时机含冷冻期
5.题目123. 买卖股票的最佳时机 III(购买两次)
6题目188. 买卖股票的最佳时机 IV(购买k次)
单纯给出题解,没有解释

public class 股票问题 {
    /*121. 买卖股票的最佳时机,(买卖一次)
    *给定一个数组prices,它的第i个元素prices[i]表示一支给定股票第 i 天的价格。
    你只能选择某一天买入这只股票,并选择在未来的某一个不同的日子卖出该股票。设计一个算法来计算你所能获取的最大利润。
    返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回0
    **/
    public int maxProfit121(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int min = Integer.MAX_VALUE, res = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] > min) {
                res = Math.max(res, prices[i] - min);
            }
            if (prices[i] < min) {
                min = prices[i];
            }
        }
        return res;
    }

    /*122. 买卖股票的最佳时机 II(无限次购买)
    *给定一个数组prices,其中prices[i]是一支给定股票第i天的价格。
    设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
    注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)
    * */
    public int maxProfit122(int[] prices) {
        if (prices.length < 1) {
            return 0;
        }
        int sum = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                sum += prices[i] - prices[i - 1];
            }
        }
        return sum;
    }

    /*714. 买卖股票的最佳时机含手续费
     *给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
     * */
    public int maxProfit714(int[] prices, int fee) {
        int buy = -prices[0], sell = 0;
        for (int price : prices) {
            int temBuy = Math.max(buy, sell - price);
            int temSell = Math.max(sell, buy + price - fee);
            buy = temBuy;
            sell = temSell;
        }
        return sell;
    }

    /*309. 最佳买卖股票时机含冷冻期
    *输入: [1,2,3,0,2]
    输出: 3
    解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
    * */
    public int maxProfit309(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int sell = 0, buy = -prices[0], cool = 0;
        for (int price : prices) {
            int temBuy = Math.max(buy, cool - price);
            int temSell = Math.max(sell, buy + price);
            int temCool = sell;
            buy = temBuy;
            sell = temSell;
            cool = temCool;
        }
        return sell;
    }

    /*123. 买卖股票的最佳时机 III(购买两次)
    *给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
    设计一个算法来计算你所能获取的最大利润。你最多可以完成两笔交易。
    注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    * */
    public int maxProfit123(int[] prices) {
        int firstSell = 0, firstBuy = Integer.MIN_VALUE;
        int secondSell = 0, secondeBuy = Integer.MIN_VALUE;
        for (int value : prices) {
            firstBuy = Math.max(firstBuy, 0 - value);
            firstSell = Math.max(firstSell, firstBuy + value);
            secondeBuy = Math.max(secondeBuy, firstSell - value);
            secondSell = Math.max(secondSell, secondeBuy + value);
        }
        return secondSell;
    }

    /*188. 买卖股票的最佳时机 IV(购买k次)
    *输入:k = 2, prices = [2,4,1],输出:2
    解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2天 (股票价格 = 4)的时候卖出,
    这笔交易所能获得利润 = 4-2 =2
    * */
    public int maxProfit188(int k, int[] prices) {
        if (k < 1 || prices.length < 2) {
            return 0;
        }
        if (k >= prices.length / 2) {
            return greedyProfit(k, prices);
        }
        int[][] dp = new int[k][2];// 0买,1卖
        for (int i = 0; i < k; i++) {
            dp[i][0] = Integer.MIN_VALUE;
        }
        for (int price : prices) {
            dp[0][0] = Math.max(dp[0][0], -price);
            dp[0][1] = Math.max(dp[0][1], dp[0][0] + price);
            for (int i = 1; i < k; i++) {
                dp[i][0] = Math.max(dp[i][0], dp[i - 1][1] - price);
                dp[i][1] = Math.max(dp[i][1], dp[i][0] + price);
            }
        }
        return dp[k - 1][1];
    }

    private int greedyProfit(int k, int[] prices) {
        int sum = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                sum += prices[i] - prices[i - 1];
            }
        }
        return sum;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值