leetcode每日一题买卖股票的最佳时机III

在这里插入图片描述

    public int maxProfit(int[] prices) {
        long[][] dp = new long[prices.length + 1][5];
        for (long[] dayStatus : dp) {
            Arrays.fill(dayStatus, Integer.MIN_VALUE);
        }
        dp[0][0] = 0; // 第零天的“没买过”状态的收入值是 0,其他都为 Integer.MIN_VALUE 表示没计算过
        for (int i = 1; i < dp.length; i++) {
            dp[i][0] = 0;
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i - 1]);
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i - 1]);
            dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i - 1]);
            dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i - 1]);
        }
        int res = Integer.MIN_VALUE;
        for (int i = 0; i < dp[prices.length].length; i++) {
            res = Math.max(res, (int)dp[prices.length][i]);
        }
        return res;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值