best time to Buy and Sell Stock III
Question Solution
Say you had an array for which the ith element was the price of a given stock on day I.
Design an algorithm to find the maximum profit. You are in most of the transactions.
Note:
Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).
Answer:
1. Scan from left to right to calculate the maximum profit for this interval of 0-i. Method can be found in stock first question
2. Scan from right to left to calculate the maximum profit for this interval of I-len. method as above.
3. Sweep through each node, plus the profit on the left and right. Record the maximum value.
Copy a little bit of someone else's explanation:
O (n^2) algorithm is easy to think of:
Find a Point J, the original price[0..n-1] divided into PRICE[0..J] and price[j. N-1], respectively, to find the maximum profit of two paragraphs.
To optimize:
For the point j+1, price[0..j+1] maximum profit, a lot of work is repeated, in the PRICE[0..J] the largest profit has been done.
Similar to the best time to Buy and Sell Stock, the maximum profit of price[0..j+1] can be introduced at O (1) from PRICE[0..J].
But how to get from Price[j. N-1] launched Price[j+1..n-1]? In turn thinking, we can use O (1) time by price[j+1..n-1] to launch price[j. N-1].
The final algorithm:
Array L[i] records the maximum profit of PRICE[0..I],
Array R[i] records the price[i. N] the maximum profit.
Known L[i], seeking l[i+1] is simple, also known r[i], R[i-1] is also very easy.
Finally, we use O (n) time to find the largest l[i]+r[i], which is the problem. (The final step can be combined in the second step).
ref:http://blog.csdn.net/pickless/article/details/12034365
Code Listing 1:
1 Public intMaxprofit (int[] prices) {2 if(Prices = =NULL|| Prices.length = = 0) {3 return0;4 }5 6 intLen =prices.length;7 int[] left =New int[Len];8 int[] right =New int[Len];9 Ten intMin = prices[0]; OneLeft[0] = 0; A for(inti = 1; i < Len; i++) { -Min =math.min (min, prices[i]); -Left[i] = Math.max (Left[i-1], prices[i]-min); the } - - intmax = Prices[len-1]; -Right[len-1] = 0; + for(inti = len-2; I >= 0; i--) { -Max =Math.max (Max, prices[i]); +Right[i] = Math.max (right[i + 1], max-prices[i]); A } at - intRST = 0; - for(inti = 0; i < Len; i++) { -rst = Math.max (RST, left[i] +right[i]); - } - in returnrst; -}
View Code
Code Listing 2:
1 Public classSolution {2 Public intMaxprofit (int[] prices) {3 if(Prices = =NULL) {4 return0;5 }6 7 intRET = 0;8 9 intLen =prices.length;Ten int[] Leftprofile =New int[Len]; One intProfile = 0; A - intMin =Integer.max_value; - for(inti = 0; i < Len; i++) { theMin =math.min (min, prices[i]); -Profile = Math.max (profile, Prices[i]-min); -Leftprofile[i] =Profile ; - } + - intMax =Integer.min_value; +Profile = 0; A for(inti = len-1; I >= 0; i--) { atMax =Math.max (Max, prices[i]); -Profile = Math.max (profile, Max-prices[i]); - - //Sum the left profit and the right profit. -ret = Math.max (ret, profile +leftprofile[i]); - } in - returnret; to } +}
View Code
DP idea:
1 //DP Solution:2 Public intMaxprofit (int[] prices) {3 if(Prices = =NULL|| Prices.length = = 0) {4 return0;5 }6 7 intRET = 0;8 9 intLen =prices.length;Ten int[] Leftprofile =New int[Len]; One A intMin = prices[0]; -Leftprofile[0] = 0; - for(inti = 1; i < Len; i++) { theMin =math.min (min, prices[i]); -Leftprofile[i] = Math.max (Leftprofile[i-1], prices[i]-min); - } - + intMax =Integer.min_value; - intProfile = 0; + for(inti = len-1; I >= 0; i--) { AMax =Math.max (Max, prices[i]); atProfile = Math.max (profile, Max-prices[i]); - - //Sum the left profit and the right profit. -ret = Math.max (ret, profile +leftprofile[i]); - } - in returnret; -}
View Code
GitHub Code Links
Leetcode:best time to Buy and Sell Stock III problem Solving report