P19148 [GeeksforGeeks] Buy stock with cooldown
普及
题目描述

You are given the prices of stock for n number of days. every ith day tell the price of the stock on that day. find the maximum profit that you can make by buying and selling stock with the restriction of After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Example:
Input:
5
1 2 3 0 2
Output:
3
Explanation:
You buy on 1st day and sell on the second day then cooldown, then buy on the fourth day and
sell on the fifth day and earn a profit of 3.
Your Task:
You don't have to read input or print anything. Your task is to complete the function maximumProfit() which takes the integer n and array prices and returns the maximum profit that can earn.
Expected Time Complexity: O(n)
Expected Space Complexity: O(n)
Constraint:
1<=n<=10^{5}
1<=prices[i]<=10^{5}

输入格式

见题面

输出格式

见题面

样例输入
5 1 2 3 0 2
样例输出
3