买卖股票最佳时机含冷冻期
卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)
数组定义: dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]
四种状态分析:
递推公式:
达到状态1: dp[i][0]
操作一:
前一天就是持有股票状态: dp[i - 1][0]
操作二: 今天买入了
a. 前一天是冷冻期: dp[i - 1][3] - prices[i]
b. 前一天是保持卖出股票状态: dp[i - 1][1] - prices[i]
(然后先给操作二取最大值, 然后max全部)
达到状态2: dp[i][1]
操作一:
前一天就是状态二: dp[i - 1][1]
操作二:
前一天就是冷冻期: dp[i - 1][3]
达到状态3: dp[i][2]
昨天一定是买入股票状态: dp[i - 1][0] + prices[i]
达到状态4: dp[i][3]
昨天一定是卖出了股票: dp[i - 1][2]
初始化:
0, 1, 2, 3四个状态, 只有0是买入, 所以给他 = -prices[0]就行
最后return的时候, 也要去状态二三四的最大值: 卖出两种先比
看代码: 做出来的决定因素就是能够理解四个状态和递推方程
- class Solution {
- public int maxProfit(int[] prices) {
- int len = prices.length;
- if(len == 0 || prices == null){
- return 0;
- }
- //create the dp
- int[][] dp = new int[len + 1][4];
- //inni, only 0 is the state of buy
- dp[0][0] = -prices[0];
- for(int i = 1; i < len; i++){
- //0--buy/have stocks 1--sold stock no cold 2--sell stock today 3--cold
- //dp[i][0]means have stocks--buy today-(cold before/normal before)/already have
- dp[i][0] = Math.max(Math.max(dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i]), dp[i - 1][0]);
- //1-be able to sell, but haven't --- cold before/same as before
- dp[i][1] = Math.max(dp[i - 1][3], dp[i - 1][1]);
- //2-sell today means: sell today get cash and yesterday have stock
- dp[i][2] = dp[i - 1][0] + prices[i];
- //3-cold--yesterday must be sell
- dp[i][3] = dp[i - 1][2];
- }
- return Math.max(Math.max(dp[len - 1][1], dp[len - 1][2]), dp[len - 1][3]);
- }
- }
一维数组优化版, 把日子后移一天
- class Solution {
- public int maxProfit(int[] prices) {
- int len = prices.length;
- if(len == 0 || prices == null){
- return 0;
- }
- //create the dp
- int[] dp = new int[4];
- //inni, only 0 is the state of buy
- dp[0] = -prices[0];
- //这里变成<=len, 因为下面没有i维了
- for(int i = 1; i <= len; i++){
- //接下俩的买入和卖出会不断变化, 所以要用temp保存
- int temp0= dp[0];
- int temp2 = dp[2];
- //还记得之前转换一维怎么做的吗, 把日子后移一天, 所以price[i-1]
- dp[0] = Math.max(Math.max(dp[3] - prices[i - 1], dp[1] - prices[i - 1]), dp[0]);
- dp[1] = Math.max(dp[3], dp[1]);
- dp[2] = temp0 + prices[i - 1];
- dp[3] = temp2;
- }
- return Math.max(Math.max(dp[1], dp[2]), dp[3]);
- }
- }
买卖股票最佳时机含手续费
笔交易都要交等于fee的手续费
贪心算法
(把fee包含在股票价格里, 然后需要定义一个反悔操作)
动态规划
和买卖股票II的唯一区别, 就是减去手续费就行了
不持有股票的情况: MAX(之前就卖了, 今天卖的-fee)
- class Solution {
- public int maxProfit(int[] prices, int fee) {
- int len = prices.length;
- if(len == 0 || prices == null){
- return 0;
- }
- int[][] dp = new int[len + 1][2];
- //浅浅初始化一波
- dp[0][0] = -prices[0];
- for(int i = 1; i < len; i++){
- //buy today/ bought before
- dp[i][0] = Math.max(dp[i - 1][1] - prices[i], dp[i - 1][0]);
- //sell today/ sought before
- dp[i][1] = Math.max(dp[i - 1][0] + prices[i] - fee, dp[i - 1][1]);
- }
- return dp[len - 1][1];
- }
- }
同样可以一维
- class Solution {
- public int maxProfit(int[] prices, int fee) {
- int len = prices.length;
- if(len == 0 || prices == null){
- return 0;
- }
- int[] dp = new int[2];
- //浅浅初始化一波
- dp[0] = -prices[0];
- for(int i = 1; i <= len; i++){
- //buy today/ bought before
- dp[0] = Math.max(dp[1] - prices[i - 1], dp[0]);
- //sell today/ sought before
- dp[1] = Math.max(dp[0] + prices[i - 1] - fee, dp[1]);
- }
- return dp[1];
- }
- }