Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Best Time to Buy and Sell Stock with Cooldown
Bookmark
Input
Standard
Single day
Longer
Small
Custom
prices
=
[1, 2, 3, 0, 2]
prices
held
holding a stock
sold
just sold today
rest
idle / cooldown
track 3 states per day, you can only buy from rest, and selling forces a cooldown
prices
held
holding a stock
sold
just sold today
rest
idle / cooldown
track 3 states per day, you can only buy from rest, and selling forces a cooldown
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
0
0
read day 0:
held=-1
,
sold=0
,
rest=0
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
0
1
0
+ 2
sold
=
-1
+
2
=
1
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
0
1
0
0
read day 1:
held=-1
,
sold=1
,
rest=0
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
-1
0
1
2
0
0
+ 3
sold
=
-1
+
3
=
2
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
-1
0
1
2
0
0
1
read day 2:
held=-1
,
sold=2
,
rest=1
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
-1
1
0
1
2
-1
0
0
1
+ 0
sold
=
-1
+
0
=
-1
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
-1
1
0
1
2
-1
0
0
1
2
read day 3:
held=1
,
sold=-1
,
rest=2
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
-1
1
1
0
1
2
-1
3
0
0
1
2
+ 2
sold
=
1
+
2
=
3
prices
1
day 0
2
day 1
3
day 2
0
day 3
2
day 4
i
held
holding a stock
sold
just sold today
rest
idle / cooldown
-1
-1
-1
1
1
0
1
2
-1
3
0
0
1
2
2
answer = max(
3
,
2
) =
3
algo
master
.
io
Step:
Start: stock prices = [1, 2, 3, 0, 2], one buy/sell at a time with a 1-day cooldown after selling
0 / 18
Input
Standard
Single day
Longer
Small
Custom
prices
=
[1, 2, 3, 0, 2]
0 / 18
algo
master
.
io
Step:
Start: stock prices = [1, 2, 3, 0, 2], one buy/sell at a time with a 1-day cooldown after selling