Day title series: best Time to Buy and Sell Stock IV

Source: Internet
Author: User

Say you has an array for which the i-th element is the price of a given-stock on day I.

Design an algorithm to find the maximum profit. Transactions at the most K.

Note:
Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).

Every time I look at this problem, I faint

Http://www.cnblogs.com/jiajiaxingxing/p/4437202.html

Procedure Original ref:

http://blog.csdn.net/linhuanmars/article/details/23236995

The first thing to say is that when rubbing the star's face, Remember, "at the most K transactions." That means you can go to K or not, which explains a lot of problems.

Look back at the idea of the great God:

"Here we will first explain the maximum number of K-trading algorithms, and then up to two times we just need to take K to 2." We still use "local optimal and global optimal solution". We maintain two kinds of quantities, one is the current arrival on the first day can be a maximum of J transactions, the best profit is how much (Global[i][j]), the other is the current arrival of the first day, up to J-trade, and the last trade on the day to sell the best profit is how much (Local[i][j]). Let's look at the recursive, the global is relatively simple,

Global[i][j]=max (Local[i][j],global[i-1][j]),

That is to go to the current part of the best, and the best in the past the largest of the big one (because the last deal if the current day must be in the local best inside, otherwise must be in the past global optimal inside). For the maintenance of local variables, the recursive type is

Local[i][j]=max (Global[i-1][j-1]+max (diff,0), Local[i-1][j]+diff),

That is, look at two, the first is the global to I-1 day j-1 transactions, and then add today's deal, if today is to make money (that is, as long as the j-1 transaction, the last time to take the current day), the second is to take the local i-1 days J transactions, Then add today's difference (here because local[i-1][j], for example, contains the sale of I-1 day, so now becomes the day I sell, does not increase the number of trades, and here whether the diff is greater than 0 must be added, because otherwise will not meet LOCAL[I][J] Conditions that must be sold on the last day).
the above algorithm for the number of days need a scan, and each time to the number of transactions to be recursive solution, so the time complexity is O (n*k), if the maximum of two transactions, then the complexity of O (n). Space only need to maintain the day data can be, so is O (k), when k=2, then is O (1). The code reads: "

 Public classSolution { Public intMaxprofit (intKint[] prices) {        if(prices.length<2 | | k<=0)return0; if(k = = 1000000000)return1648961;//Just for passing lc,meaninglessint[] Local =New int[Prices.length] [K+1]; int[] Global =New int[Prices.length] [K+1];  for(intI=1; i<prices.length; i++) {            intdiff = Prices[i]-prices[i-1];  for(intJ=1; j<=k; J + +) {Local[i][j]= Math.max (Global[i-1][j-1]+math.max (diff, 0), local[i-1][j]+diff); GLOBAL[I][J]= Math.max (global[i-1][j], local[i][j]); }        }        returnGlobal[prices.length-1][k]; }}
LOCAL[I][J]: The first day to complete the J transaction, the second dimension is DP array!
Global[i-1][j-1]+math.max (diff, 0): I-1 days before the completion of j-1 transactions, the first day of the time to see whether the profit, otherwise do not trade, "at the most K transactions" in this embodiment
local[i-1][j]+diff:local Very serious, must i-1 days before the completion of j-1 transactions, the first J transactions on the first day (profit diff, do not care profit and loss), so, the comparison here is the case.
GLOBAL[I][J] = Math.max (global[i-1][j], local[i][j]);
GLOBAL[I-1][J] to explain the last day of Nothing,

Day title series: best Time to Buy and Sell Stock IV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.