首页 > Java > java教程 > 正文

LeetCode Day动态编程第8部分

WBOY
发布: 2024-07-17 11:39:09
原创
1026 人浏览过

LeetCode DayDynamic Programming part8

121. 买卖股票的最佳时机

给你一个数组价格,其中prices[i]是给定股票第i天的价格。

您希望通过选择某一天买入一只股票并选择未来的另一天卖出该股票来最大化您的利润。

返回您可以从本次交易中获得的最大利润。如果无法获得任何利润,则返回0。

示例1:

输入:价格 = [7,1,5,3,6,4]
输出:5
说明:第 2 天买入(价格 = 1),第 5 天卖出(价格 = 6),利润 = 6-1 = 5。
请注意,不允许在第 2 天买入并在第 1 天卖出,因为您必须在卖出之前买入。
示例2:

输入:价格 = [7,6,4,3,1]
输出:0
说明:在这种情况下,没有进行任何交易,最大利润 = 0。

限制:

1 0 原始页面

方法一、贪心算法

    public int maxProfit(int[] prices) {
        if(prices.length == 0){
            return 0;
        }

        int profit = 0;
        int buy = prices[0];
        for(int i=1; i<prices.length; i++ ){
            if(buy>=prices[i]){
                buy = prices[i];
            }else{
                profit = Math.max(profit, prices[i]-buy);
            }
        }
        return profit;
    }
登录后复制

时间 O(n) 空间 O(1)

方法2 动态规划

    public int maxProfit(int[] prices) {
        if(prices.length == 0){
            return 0;
        }

        // 2 means we have 2 different status (have owned stock or not )
        int[][] dp = new int[prices.length][2];

        // if we want to own the stock we should pay for the specific price
        dp[0][0] = -prices[0];
        for(int i=1; i<dp.length; i++){
            dp[i][0] = Math.max(dp[i-1][0], -prices[i]);
            dp[i][1] = Math.max(dp[i-1][1], prices[i] + dp[i-1][0]);
        }
        // Arrays.stream(dp).map(Arrays::toString).forEach(System.out::println);
        return dp[dp.length-1][1];
    }
登录后复制

时间 O(n) 空间 O(n)
动态规划比贪心算法更通用,因为它不仅适用于特定问题。

方法2.1(提高空间复杂度)

    public int maxProfit(int[] prices) {
        if(prices.length == 0){
            return 0;
        }

        // 2 means we have 2 different status (have owned stock or not )
        int[] dp = new int[2];

        // if we want to own the stock we should pay for the specific price
        dp[0] = -prices[0];
        for(int i=1; i<prices.length; i++){
            dp[1] = Math.max(dp[1], prices[i] + dp[0]);
            dp[0] = Math.max(dp[0], -prices[i]);
        }
        // 
        return dp[1];
    }

登录后复制

这里要小心,我们应该先处理 dp[1],因为它将使用之前的 dp[0] 而不是更新后的 dp[0]。

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

给你一个整数数组prices,其中prices[i]是给定股票第i天的价格。

每天,您都可以决定购买和/或出售股票。您在任何时候最多只能持有一股股票。不过,您可以购买并在当天立即出售。

找到并返回您可以实现的最大利润。

示例1:

输入:价格 = [7,1,5,3,6,4]
输出:7
说明:第 2 天买入(价格 = 1),第 3 天卖出(价格 = 5),利润 = 5-1 = 4。
然后在第4天买入(价格=3)并在第5天卖出(价格=6),利润=6-3=3。
总利润为 4 + 3 = 7。
示例2:

输入:价格 = [1,2,3,4,5]
输出:4
说明:第 1 天买入(价格 = 1),第 5 天卖出(价格 = 5),利润 = 5-1 = 4。
总利润为 4。
示例 3:

输入:价格 = [7,6,4,3,1]
输出:0
说明:没有办法盈利,所以我们从来不买股票来实现最大利润0。

限制:

1 0

    public int maxProfit(int[] prices) {
        if(prices.length <1){
            return 0;
        }

        int[][] dp = new int[prices.length][2];
        dp[0][0] = -prices[0];

        for(int i=1; i<prices.length; i++){
            //only when we do not own the stack we can buy a new stock
            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 dp[prices.length-1][1];
    }
登录后复制

滚动数组方法

    public int maxProfit(int[] prices) {
        if(prices.length <1){
            return 0;
        }

        int[] dp = new int[2];
        dp[0] = -prices[0];

        for(int i=1; i<prices.length; i++){
            //only when we do not own the stack we can buy a new stock
            dp[1] = Math.max(dp[0]+prices[i], dp[1]);
            dp[0] = Math.max(dp[1]-prices[i], dp[0]);

        }

        return dp[1];
    }
登录后复制

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

给你一个数组价格,其中prices[i]是给定股票第i天的价格。

找到你能获得的最大利润。您最多可以完成两笔交易。

注意:您不得同时进行多项交易(即您必须先卖出股票才能再次购买)。

示例1:

输入:价格 = [3,3,5,0,0,3,1,4]
输出:6
说明:第 4 天买入(价格 = 0),第 6 天卖出(价格 = 3),利润 = 3-0 = 3。
然后在第 7 天买入(价格 = 1)并在第 8 天卖出(价格 = 4),利润 = 4-1 = 3。
示例2:

输入:价格 = [1,2,3,4,5]
输出:4
说明:第 1 天买入(价格 = 1),第 5 天卖出(价格 = 5),利润 = 5-1 = 4。
请注意,您不能在第一天买入,在第二天买入并随后卖出,因为您同时进行多项交易。您必须先卖出才能再次购买。
示例 3:

输入:价格 = [7,6,4,3,1]
输出:0
说明:在这种情况下,没有进行任何交易,即最大利润 = 0。

限制:

1 0

    public int maxProfit(int[] prices) {
        int transactions = 2;
        int[][] dp = new int[prices.length][transactions*2+1];
        for(int i=1; i<=transactions; i++){
            dp[0][i*2-1] = -prices[0];
        }

        for(int i=1; i<prices.length; i++){

            dp[i][1] = Math.max(dp[i-1][0]-prices[i], dp[i-1][1]);
            dp[i][2] = Math.max(dp[i-1][1]+prices[i], dp[i-1][2]);
            dp[i][3] = Math.max(dp[i-1][2]-prices[i], dp[i-1][3]);
            dp[i][4] = Math.max(dp[i-1][3]+prices[i], dp[i-1][4]);
        }
        Arrays.stream(dp).map(Arrays::toString).forEach(System.out::println);

        return dp[prices.length-1][4];
    }
登录后复制

以上是LeetCode Day动态编程第8部分的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!