Leetcode_714_买卖股票的最佳时机_动态规划

12/17

设立两种状态,一种是持有股票的状态,一种是没有股票的状态,然后进行状态转移
后面有详细的思维过程

最终代码如下

class Solution {
    public int maxProfit(int[] prices, int fee) {
        //no是不持有,yes是持有股票
        int no = 0;
        int yes = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            int befno = no;
            no = Math.max(yes + prices[i] - fee, no);
            yes = Math.max(yes, befno - prices[i]);
        }
        return no;
    }
}

步骤一:

观察到无法使用贪心,需要用动态规划解决此问题

步骤二:

思考状态的转移,如果将目前拥有的钱数作为状态,则状态过多,时间复杂度太大
将前一个买卖点作为状态(大概也行,用奇偶判断一下是买入还是卖出),也不是很好
最终思考出,将目前是否持有股票作为状态,比较合理。

步骤三:

写出动态规划状态转移的基本框架
class Solution {
    public int maxProfit(int[] prices, int fee) {
        int ans=0;
        int n=prices.length;
        int [][]dp=new int[n][2];
        //0是不持有,1是持有股票
        dp[0][0]=0;
        dp[0][1]=-prices[0];
        for(int i=1;i<n;i++){
            dp[i][0]=0;
            dp[i][1]=-prices[i];
            for(int j=0;j<i;j++){
               //状态转移;
               dp[i][0]=Math.max(dp[j][0],dp[i][0]);
               dp[i][0]=Math.max(dp[j][1]+prices[i]-fee,dp[i][0]);
               dp[i][1]=Math.max(dp[j][1],dp[i][1]);
               dp[i][1]=Math.max(dp[j][0]-prices[i],dp[i][1]);
               //更新ans;
               ans=Math.max(dp[i][0],ans);
            }
        }
        return ans;
    }
}

步骤四:

思考到状态转移有冗余的操作,只需要由i-1的状态转移过来即可,
dp[i-1]是前i-1个股票的最优解。
在这里插入代码片class Solution {
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        //0是不持有,1是持有股票
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][1] + prices[i] - fee, dp[i - 1][0]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[n - 1][0];
    }
}

步骤六:

题目对前i个股票的最优解不关心(也就是不要求前面的状态结果)
而状态转移也只是从前一个状态转移过来
所以干脆用两个数存前面两个状态来节省空间
class Solution {
    public int maxProfit(int[] prices, int fee) {
        //no是不持有,yes是持有股票
        int no = 0;
        int yes = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            int befno = no;
            no = Math.max(yes + prices[i] - fee, no);
            yes = Math.max(yes, befno - prices[i]);
        }
        return no;
    }
}

优化的提升
在这里插入图片描述

标程做法

略有不同,没有保存之前买股票的状态,而是设置了buy来存储状态
class Solution {
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int sell = 0, buy = -prices[0];
        for (int i = 1; i < n; ++i) {
            sell = Math.max(sell, buy + prices[i] - fee);
            buy = Math.max(buy, sell - prices[i]);
        }
        return sell;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-han-sh-rzlz/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值