Best Sell Stock III (JAVA)-Dynamic planning

Source: Internet
Author: User

topic:
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. Complete at most two transactions.
Note:
The May isn't engage in multiple transactions at the same time (ie, your must sell the stock before your buy again).

Translation:
There is an array of the daily price of the stock, you can trade a total of two times, but each time can only buy one or sell a stock, and the hands can only have a stock, that is, before buying a second stock, you must first sell the first stock. Design an algorithm to maximize revenue

For example, an array prices = {3,2,6,5,0,3} represents the daily price of a stock, and the maximum benefit is: (6-2) + (3-0) = 7;

idea:
1, the assumption can only trade once, to find the maximum profit before the day, with an array of profit, for example, for the above price, can be calculated profit={0,0,4,4,4,4}
2, in 1 of the method, from the forward calculation of the maximum income reverseprofit={4,4,3,3,3,0}
3, the logarithm group carries on the prices circulation, calculates profit[i]+reverseprofit[i] the maximum value, namely is the maximum income

Code (Java)

public class Solution {public int maxprofit (int[] prices) {int nlen = prices.length;
        if (nlen<2) {return 0;
        } int[] Profit = new Int[nlen];
        int minprice = prices[0];
        int maxprofit = 0; for (int i = 1; i<nlen; i++) {//Calculate the maximum proceeds once sold if (Prices[i] < Minprice) {Profit[i] =
                PROFIT[I-1];
            Minprice = Prices[i];
                }else{int temp = prices[i]-minprice; Profit[i] = profit[i-1] > temp?
            PROFIT[I-1]: temp;
        } if (Nlen = 2) {//If only three days of data, only transaction once, do not do the following calculation of return profit[1];
        int maxprice = prices[nlen-1];
        int[] Reverseprofit = new Int[nlen]; for (int i = nLen-1 i > 1; i--) {//reverse find out the maximum gain for a transaction if (Prices[i-1] > Maxprice) {reversepro
                 FIT[I-1] = Reverseprofit[i];
             Maxprice = Prices[i-1];
 }else{                int temp = maxprice-prices[i-1]; REVERSEPROFIT[I-1] = reverseprofit[i] > temp?
             Reverseprofit[i]: temp;
            for (int i = 0; i<nlen; i++) {int temp = Profit[i] + reverseprofit[i]; Maxprofit = maxprofit > Temp? 
        Maxprofit:temp;
    return maxprofit; }
}

Search for improved version (simpler code, less space complexity):

public class Solution {public
    int maxprofit (int[] prices) {
        int hold1 = integer.min_value, hold2 = Integer.min_va LUE;
        int release1 = 0, release2 = 0;
        for (int i:prices) {                              
            release2 = Math.max (Release2, hold2+i);    
            Hold2    = Math.max (hold2,    release1-i);  
            Release1 = Math.max (release1, hold1+i);   
            Hold1    = Math.max (hold1,-i    );         
        }
        Return Release2 
    }
}

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.