1179. Buy and Sell with Cooldown

MediumDynamic ProgrammingStock DP

Given daily stock prices, you may complete unlimited transactions, but after selling you must wait one day (cooldown) before buying again. Return the maximum profit. The input is JSON {prices}.

Input: JSON {prices}.

Output: Integer — the maximum profit.

Examples

Example 1
Input: {"prices":[1,2,3,0,2]}
Output: 3
Explanation: Buy, sell, cooldown, buy, sell.
Example 2
Input: {"prices":[1]}
Output: 0
Explanation: No transaction.
Example 3
Input: {"prices":[2,1,4]}
Output: 3
Explanation: Buy at 1, sell at 4.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →