dp- stock issue summary

the easiest:

122. The best time to buy and sell stocks II  (capable of trading numerous times)

Given an array, which i-th element is a given stock i-day prices.

Design an algorithm to compute the maximum profit you can get. You can close more deals (buy and sell a stock) as much as possible.

Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).

class Solution {
 public :
     int MAXPROFIT (Vector < int > & Prices) {
         int n-= prices.size ();
         IF (n-<= 0 ) return  0 ;
         / * int DP [2] [n-]; and to buy sell two states:
        memset(dp,0,sizeof(dp));
        dp[0][0] = -prices[0];
        dp[1][0] = 0;
        for(int i = 1; i < n; i++){
            dp[0][i] = max(dp[0][i-1],dp[1][i-1] - prices[i]);
            dp[1][i] = max(dp[0][i-1] + prices[i],dp[1][i-1]);
        }
        DP return [. 1] [n--. 1]; * / 
    // space optimization
int HOLD, Sell; hold = -prices[0]; sell = 0; for (int i = 1; i < n; i++){ int temp = hold; hold = max(hold,sell - prices[i]); sell = max(sell,temp + prices[i]); } return sell; } };

121. The best time to stock trading (trading can once)

Given an array, which i-th element is a given stock i-day prices.

If you are only allowed up to complete a transaction (ie buying and selling a stock), to design an algorithm to compute the maximum profit you can get.

Note that you can not sell the stock before buying stocks.

Before more than one state:

class Solution {
 public :
     int MAXPROFIT (Vector < int > & Prices) {
         int n-= prices.size ();
         IF (n-== 0 ) return  0 ;
         int DP [n-] [ 2 ] [ 2 ]; // multiple a status, the remaining number of trading 
        Memset (DP, 0xBF , the sizeof (DP));
         // initialize the buying and selling day 1; 
        DP [ 0 ] [ 0 ] [ 0 ] = -prices [ 0 ];
        DP [ 0 ] [ . 1 ] [ . 1 ] = 0 ;
         for ( int I = . 1 ; I <n-; I ++ ) {
             // buy and a left is impossible 
            DP [I] [ 0 ] [ 0 ] = max (DP [I- . 1 ] [ . 1 ] [ . 1 ] -prices [I], DP [I- . 1 ] [ 0 ] [ 0 ]);
            dp[i][1][0] = max(dp[i-1][0][0]+prices[i],dp[i-1][1][0]);
            dp[i][1][1] = dp[i-1][1][1];
        }
        return max(dp[n-1][1][0],0);
    }
};

Scroll array of optimization:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if(n == 0) return 0;
        int dp[2][2];
        memset(dp,0xbf,sizeof(dp));
        dp[0][0] = -prices[0];
        dp[1][1] = 0;
        for(int i = 1; i < n;i++){
            int temp[2][2];
            temp[0][0] = max(dp[0][0],dp[1][1] - prices[i]);
            temp[1][0] = max(dp[0][0]+prices[i],dp[1][0]);
            dp[0][0] = temp[0][0];
            dp[1][0] = temp[1][0];
        }
        return max(dp[1][0],0);
    }
};

III 123. the best time to buy and sell stocks

Given an array, which i-th element is the price of a given stock i-th day.

Design an algorithm to compute the maximum profit you can get. You can complete up to two transactions.

Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).

Direct rolling array optimized version: dp [0] [j] represents the lower left j times to buy state; dp [1] [j] represents the left j times to selling state;

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if(n == 0) return 0;
        vector<vector<int>> dp(2,vector<int>(3,0xbfbfbfbf));
        vector<vector<int>> temp(2,vector<int>(3,0xbfbfbfbf));
        dp[0][1] = -prices[0];
        dp[1][2] = 0;
        for(int i = 1; i < n; i++){
            temp[0][0] = max(dp[0][0],dp[1][1]-prices[i]);
            temp[0][1] = max(dp[0][1],dp[1][2]-prices[i]);
            //temp[0][2]不可能;
            temp[1][0] = max(dp[1][0],dp[0][0]+prices[i]);
            temp[1][1] = max(dp[1][1],dp[0][1]+prices[i]);
            temp[1][2] = dp[1][2];
            dp = temp;
        }
        return max(dp[1][2],max(dp[1][1],dp[1][0]));
    }
};

188. The best time to buy and sell stocks IV (basic template)

Given an array, which i-th element is the price of a given stock i-th day.

Design an algorithm to compute the maximum profit you can get. You can complete most k transactions.

Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).

class Solution {
 public :
     int MAXPROFIT ( int k, Vector < int > & Prices) {
         // size k to be classified discussion: 
        int n-= prices.size ();
         IF (n-== 0 || k == 0 ) return  0 ;
         IF (K> n-/ 2 ) { // K> when n / 2, corresponding to the transaction may be many times; 
            int Sell, HOLD, TEMP;
            sell = 0;
            hold = -prices[0];
            for(int i = 1; i < n; i++){
                temp = hold;
                hold = max(hold,sell-prices[i]);
                sell = max(sell,temp+prices[i]);
            }
            return sell;
        }
        vector<vector<int>> dp(2,vector<int>(k+1,0xbfbfbfbf));
        vector<vector<int>> temp(2,vector<int>(k+1,0xbfbfbfbf));
        dp[0][k-1] = -prices[0];
        dp[1][k] = 0;
        for(int i = 1; i < n; i++){
            for(int j = 0; j < k; j++)
                temp[0][j] = max(dp[0][j],dp[1][j+1]-prices[i]);
            for(int j = 0; j < k;j++)
                temp[1][j] = max(dp[1][j],dp[0][j]+prices[i]);
            temp[1][k] = 0;
            dp = temp;
        }
        return *max_element(dp[1].begin(),dp[1].end());
    }
};

309. The best time to buy and sell shares with the freezing of

Given an array of integers, which represents the i-th element of the i-day stock price.

Design an algorithm to calculate the maximum profit. In the following constraints, you can close more deals as possible (buy and sell a stock):

You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).
After selling the stock, you can not buy the stock the next day (ie freeze to one day).

class Solution {
 public :
     int MAXPROFIT (Vector < int > & Prices) {
         int n-= prices.size ();
         IF (n-== 0 ) return  0 ;
         // than a simple multi-state, a frozen state;
         // DP [0] Representative buy, dp [1] Representative sold, dp [2] Representative freeze 
        Vector < int > DP ( . 3 ), TEMP ( . 3 );
        dp[0] = -prices[0];
        dp[1] = dp[2] = 0;
        for(int i = 1; i < n; i++){
            temp[0] = max(dp[0],dp[2]-prices[i]);
            temp[1] = max(dp[1],dp[0]+prices[i]);
            temp[2] = dp[1];
            dp = temp;
        }
        return max(dp[1],dp[2]);
    }
};

714. The best time to buy and sell stocks with fee

Given an array of integers prices, which represent the i-th element of the i-day stock price; non-negative integer fee represents the cost of trading stocks procedures.

You can complete the transaction unlimited number of times, but you need to pay the fee for each transaction. If you have purchased a stock, you sell it before you can no longer continue to buy stocks.

Returns the maximum of profit.

At the simplest, based on the state transition equation changed just fine;

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        if(n == 0) return 0;
        int hold, sell,temp;
        hold = -prices[0]-fee;
        sell = 0;
        for(int i = 1; i < n; i++){
            temp = hold;
            hold = max(hold,sell-prices[i]-fee);
            sell = max(sell,temp+prices[i]);
        }
        return sell;
    }
};

 

Guess you like

Origin www.cnblogs.com/Aliencxl/p/12292080.html