dp[-1][k][0] = 0 解释:因为 i 是从 0 开始的,所以 i = -1 意味着还没有开始,这时候的利润当然是 0 。 dp[-1][k][1] = -infinity 解释:还没开始的时候,是不可能持有股票的,用负无穷表示这种不可能。 dp[i][0][0] = 0 解释:因为 k 是从 1 开始的,所以 k = 0 意味着根本不允许交易,这时候利润当然是 0 。 dp[i][0][1] = -infinity 解释:不允许交易的情况下,是不可能持有股票的,用负无穷表示这种不可能。
其实上面的边界,这么写起来是很别扭的,我们可以理解为综合考虑:i , k极端情况下的的边界分布。
以上三部分其实就是讲解了:状态、状态转移方程、边界。相当于提出了一套用于解决股票问题的框架。
应用
leetcode_0121
题目描述:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
交易次数仅限于一次,k=1
状态定义: $dp[i][k][j] , 0 < i < len(prices) , 0<=k<=1$ 表示第 i 天 ,第 K 次交易 , 持有或不持有,所能获得的最大利润,0 < i < len(prices) , 0<=k<=1
Say you have an array prices for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Example 2:
Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again. Example 3:
Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2:
Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again. Example 3:
Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
K限制在两次以内
状态定义:$dp[i][k][j] , 0 < i < len(prices) , 0<=k<=2$
dp = [[[0for _ inrange(2)] for _ inrange(k+1)] for _ inrange(len(prices)+1)] for k inrange(k+1): dp[0][k][0] = 0 dp[0][k][1] = float("-inf") for i inrange(len(prices)+1): dp[i][0][0] = 0 dp[i][0][1] = float("-inf") for i inrange(1,len(prices)+1): for k inrange(1,k+1): dp[i][k][1] = max( dp[i-1][k][1] , dp[i-1][k-1][0] - prices[i-1] ) #第 i 天 k 次交易 持有状态 = max( 第 i-1 天 k 次交易 持有状态,选择观望 , 第 i-1 天 k-1 次交易 未持有状态,选择买入 ) dp[i][k][0] = max( dp[i-1][k][0] , dp[i-1][k][1] + prices[i-1] ) #第 i 天 k 次交易 未持有状态 = max( 第 i-1 天 k 次交易 未持有状态,选择观望 , 第 i-1 天 k-1 次交易 持有状态,选择卖出 ) maxprofit = 0 for k inrange(k+1): temp = max(dp[-1][k]) if maxprofit < temp: maxprofit = temp return maxprofit
leetcode_0309
题目描述
1 2 3 4 5 6 7 8 9 10 11 12
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: