Skip to content

Commit c226723

Browse files
authored
Create 309. Best Time to Buy and Sell Stock with Cooldown
1 parent a432ddb commit c226723

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
//Space -> O(1)
4+
int sold = Integer.MIN_VALUE;
5+
int held = Integer.MIN_VALUE;
6+
int reset = 0;
7+
8+
//Time -> O(N)
9+
for(int p : prices) {
10+
int prevSold = sold;
11+
sold = held + p;
12+
held = Math.max(held, reset - p);
13+
reset = Math.max(reset, prevSold);
14+
}
15+
16+
return Math.max(sold, reset);
17+
}
18+
}

0 commit comments

Comments
 (0)