Best Time to Buy and Sell Stock with Cooldown – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
You are given an array prices
where prices[i]
is the price of a given stock on the ith
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:
Input: prices = [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
Example 2:
Input: prices = [1] Output: 0
Constraints:
1 <= prices.length <= 5000
0 <= prices[i] <= 1000
C++ Best Time to Buy and Sell Stock with Cooldown LeetCode Solution
int maxProfit(vector<int>& p)
{
if (p.size() < 2)
return 0;
int i, sz = p.size();
int ret = 0;
vector<int> buy(sz, 0);
vector<int> sell(sz, 0);
buy[0] = -p[0];
for (i = 1; i < sz; ++i)
{
sell[i] = max(buy[i - 1] + p[i], sell[i - 1] - p[i - 1] + p[i]);
if (ret < sell[i]) //record the max sell[i]
ret = sell[i];
if (1 == i)
buy[i] = buy[0] + p[0] - p[1];
else
buy[i] = max(sell[i - 2] - p[i], buy[i - 1] + p[i - 1] - p[i]);
}
return ret;
}
Java Best Time to Buy and Sell Stock with Cooldown LeetCode Solution
public int maxProfit(int[] prices) {
int profit1=0, profit2=0;
for(int i=1; i<prices.length; i++){
int copy=profit1;
profit1=Math.max(profit1+prices[i]-prices[i-1], profit2);
profit2=Math.max(copy, profit2);
}
return Math.max(profit1, profit2);
}
Python 3 Best Time to Buy and Sell Stock with Cooldown LeetCode Solution
def maxProfit(self, prices):
notHold, notHold_cooldown, hold = 0, float('-inf'), float('-inf')
for p in prices:
hold, notHold, notHold_cooldown = max(hold, notHold - p), max(notHold, notHold_cooldown), hold + p
return max(notHold, notHold_cooldown)
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below