LeetCode 309 - Best Time to Buy and Sell Stock with Cooldown
Difficulty: medium
Stock Series:
Part 1: LeetCode 121 - Best Time to Buy and Sell Stock
Part 2: LeetCode 122 - Best Time to Buy and Sell Stock II
Part 3: LeetCode 123 - Best Time to Buy and Sell Stock III
Part 4: LeetCode 188 - Best Time to Buy and Sell Stock IV
Part 5: LeetCode 309 - Best Time to Buy and Sell Stock with Cooldown
Part 6: LeetCode 714 - Best Time to Buy and Sell Stock with Transaction Fee
Problem Description
English (Best Time to Buy and Sell Stock with Cooldown)
You are given an array prices
where prices[i]
is the price of a given stock on the $i^{th}$ day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
- After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
1 |
|
Example 2:
1 |
|
Constraints:
1 <= prices.length <= 5000
0 <= prices[i] <= 1000
Chinese (最佳买卖股票时机含冷冻期)
给定一个整数数组 prices
,其中第 prices[i]
表示第 i
天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
- 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
1 |
|
示例 2:
1 |
|
提示:
1 <= prices.length <= 5000
0 <= prices[i] <= 1000
Solution
C++
1 |
|