Smart Stock Trader with Cooldown

Hard
dynamic-programming array state-machine
Problem Description

Given an array of stock prices, find the maximum profit you can achieve by buying and selling stocks multiple times. After selling a stock, you must wait one day (cooldown) before buying again.

Input Format
First line contains n (number of days). Second line contains n space-separated integers representing stock prices.
Output Format
Maximum profit achievable with the cooldown constraint.
Constraints
• 1 ≤ n ≤ 5000
• 0 ≤ stock prices ≤ 1000
• Must wait 1 day after selling before buying again
• Can hold at most one stock at a time
• Can perform multiple transactions
Sample Input/Output
Input:
5
1 2 3 0 2
Output:
3
Explanation

Buy on day 1 (price=1), sell on day 3 (price=3), profit=2. Wait 1 day (cooldown), buy on day 5 (price=0), sell on day 6 (price=2), profit=2. Total profit = 2+2 = 4. Wait, let me recalculate: Buy at 1, sell at 3 (profit=2), cooldown, buy at 0, sell at 2 (profit=2). Total=4. Actually, optimal is buy at 1, sell at 2 (profit=1), buy at 0, sell at 2 (profit=2). Total=3.

Solution Hints
140
Points
3000ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback