【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳買賣股票時機含冷凍期(Medium)(JAVA)
【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳買賣股票時機含冷凍期(Medium)(JAVA)
題目地址: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
題目描述:
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:
Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
題目大意
給定一個整數陣列,其中第 i 個元素代表了第 i 天的股票價格 。
設計一個演算法計算出最大利潤。在滿足以下約束條件下,你可以儘可能地完成更多的交易(多次買賣一支股票):
- 你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。
- 賣出股票後,你無法在第二天買入股票 (即冷凍期為 1 天)。
解題方法
- 分為兩種狀態: 當前未持有股票的最大值,當前未持有股票的最大值
- 假設 dp[i] 之前的狀態都是已知的,求出 dp[i][0] 和 dp[i][1]
- 當前持有股票 dp[i][0]:可能是上一次就持有了股票 dp[i - 1][0] ,也可能是當前才持有股票(因為有冷卻時間,只能隔一個的最大值 - prices[i]) 也就是 dp[i - 2] - prices[i])),求出這兩種情況的最大值
- 當前不持有股票 dp[i][1]:可能是當前這一次賣出去了 dp[i - 1][0] + prices[i](上一次必須持有股票才能賣),可能是當前啥也沒操作 dp[i - 1][1],求出這兩種情況的最大值
class Solution {
public int maxProfit(int[] prices) {
// dp[i][0]: has stock; dp[i][1]: no stock
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++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[Math.max(0, i - 2)][1] - prices[i]);
dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
}
return dp[prices.length - 1][1];
}
}
執行耗時:1 ms,擊敗了99.38% 的Java使用者
記憶體消耗:37.8 MB,擊敗了41.28% 的Java使用者
相關文章
- LeetCode 309. Best Time to Buy and Sell Stock with CooldownLeetCode
- LeetCode-Best Time to Buy and Sell Stock with CooldownLeetCode
- Best Time to Buy and Sell Stock leetcode javaLeetCodeJava
- [leetcode]Best Time to Buy and Sell StockLeetCode
- Leetcode Best Time to Buy and Sell StockLeetCode
- Best Time to Buy and Sell Stock II leetcode javaLeetCodeJava
- Best Time to Buy and Sell Stock III leetcode javaLeetCodeJava
- Leetcode Best Time to Buy and Sell Stock IILeetCode
- Best Time to Buy and Sell Stock III -- LeetCodeLeetCode
- leetcode best-time-to-buy-and-sell-stock-iii(Java)LeetCodeJava
- LeetCode(188) Best Time to Buy and Sell Stock IV (Java)LeetCodeJava
- leetcode_best-time-to-buy-and-sell-stock-iiLeetCode
- LeetCode-Best Time to Buy and Sell Stock IVLeetCode
- Leetcode-Best Time to Buy and Sell Stock IIILeetCode
- Best Time to Buy and Sell Stock系列分析
- LeetCode 714 Best Time to Buy and Sell Stock with Transaction FeeLeetCode
- 【Lintcode】393. Best Time to Buy and Sell Stock IV
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解題報告LeetCode
- 【LeetCode從零單排】No121 Best Time to Buy and Sell StockLeetCode
- 買賣股票的最佳時機 IV javaJava
- leetcode 動態規劃 買賣股票的最佳時機含手續費 javaLeetCode動態規劃Java
- 【LeetCode動態規劃#13】買賣股票含冷凍期(狀態眾多,比較繁瑣)、含手續費LeetCode動態規劃
- leetcode 121 買賣股票的最佳時機LeetCode
- Leetcode121. 買賣股票的最佳時機LeetCode
- LeetCode-Java:122. 買賣股票的最佳時機ⅡLeetCodeJava
- leetcode 123 買賣股票的最佳時機 IIILeetCode
- leetcode 122 買賣股票的最佳時機 IILeetCode
- [Leetcode]123.買賣股票的最佳時機3LeetCode
- LeetCode-122-買賣股票的最佳時機 IILeetCode
- leetcode【每日一題】122. 買賣股票的最佳時機 II JavaLeetCode每日一題Java
- LeetCode《買賣股票的最佳時機》系列題目,最詳解LeetCode
- 「leetcode」714. 買賣股票的最佳時機含手續費 超詳細講解LeetCode
- 力扣之陣列——買賣股票的最佳時機力扣陣列
- LeetCode 188 買賣股票的最佳時機IV HERODING的LeetCode之路LeetCode
- [力扣每日一題]714. 買賣股票的最佳時機含手續費力扣每日一題
- 陣列筆試題之買賣股票的最佳時機陣列筆試
- Java演算法之動態規劃詳解-買賣股票最佳時機Java演算法動態規劃
- 演算法122. 買賣股票的最佳時機 II演算法