Say you have an array for which the ith element are the price of a given the
Design a algorithm to find the maximum profit. You may complete as many transactions as (ie, buy one and sell one share of the "stock multiple times") with the FO Llowing Restrictions:
The May isn't engage in multiple transactions at the same time (ie, your must sell the stock before your buy again).
After you sell your the stock, your cannot buy the stock on next day. (ie, cooldown 1 day)
Example:
Prices = [1, 2, 3, 0, 2]
Maxprofit = 3
transactions = [Buy, Sell, cooldown, buy, sell]
Given an array, I represents the price of the stock in the first day. Ask for the maximum yield, the number of transactions you want to make, and you can buy and sell the same stock multiple times. But one stock can only be traded at a time (not multiple at the same time) and must be sold before it can be bought and a day's rest after sale.
The idea of solving problems: Dynamic programming
Code:
public int Maxprofit (int[] prices) {int sell = 0, Prev_sell = 0, buy = integer.min_value, prev_buy;
for (int price:prices) {prev_buy = buy;
Buy = Math.max (Prev_sell-price, prev_buy);
Prev_sell = sell;
Sell = Math.max (prev_buy + price, Prev_sell);
return sell; }