Leetcode series problem solving ideas

I have been writing questions for a while, and I have come to this series. Write some experience that I can understand. It is not optimal, but I think it is not too difficult to understand.
To return to the battle, the general idea of ​​this series is to give you a series of stock prices, plus various restrictions, so that you can find the maximum profit under each restriction. (If you have this ability to predict the future, you don’t need to move bricks, orz...)

I. Restrictions: Only one transaction is allowed;

Idea: First calculate the profit of each transaction, positive means surplus, negative means loss, the problem is converted to a cumulative array to find the maximum value of a continuous sub-array, the code is as follows:

int maxProfiti(vector<int>& prices){
    
    

 int len = prices.size(), res = 0, i;

 if(len < 2) return 0;

    vector<int> profit(len, 0);

 for(i = 0; i < len-1; i++)

        profit[i] = prices[i+1]-prices[i];

 for(i=len-2; i >=0; i--){
    
    

 if(profit[i+1]>0)

            profit[i] += profit[i+1];

        res = res > profit[i] ? res: profit[i];

 }

    return res;

}

II. Restrictions: allow as many transactions as possible;

Idea: Same as above, find the profit array, then traverse, add positive numbers, and pass negative numbers. The code is as follows:

int maxProfitii(vector<int>& prices){
    
    

 int len = prices.size(), res = 0, i;

 if(len < 2) return 0;

    vector<int> profit(len, 0);

 for(i = 0; i < len-1; i++)

        profit[i] = prices[i+1]-prices[i];

 for( i = 0; i < len; i++){
    
    

 if(profit[i] > 0)

            res += profit[i];

 }

    return res;

}

Insert picture description here

I have summarized some of the interview questions needed by the first-line Internet companies, you can add Junyang: 832218493 or the public number Zero Sound Academy to get it for free!

III. Restrictions, a maximum of two transactions are allowed;

Idea: I originally saw this question and wanted to use the idea of ​​solving the problem in question I above. dp[i][j] means the maximum profit of only one transaction from i to j (inclusive), but the time complexity is too high. In the discussion area later, it suddenly became clear that dp[k][i] represents the maximum profit when up to the price i and allows up to k transactions. The recursive formula is dp[k][i]=max(dp[k][i -1], prices[i]-prices[j]+dp[k-1][j]), the range of j is [0, i], the code is as follows:

int maxProfitiii(vector<int>& prices){
    
    

 int len = prices.size(), K = 2, temp, k, i;

 if(len < 2) return 0;

    vector<vector<int>> dp(K+1, vector<int>(len, 0));

 for(k = 1; k <= K; k++){
    
    

        temp = dp[k-1][0] - prices[0];

 for(i = 1; i < len; i++){
    
    

            dp[k][i] = max(dp[k][i-1], prices[i] +temp);

            temp = max(temp, dp[k-1][i]-prices[i]);

 }

 }

    return dp[K][len-1];

}

I played a trick in the code and removed the time-increasing code block traversing dp[k-1][0] to dp[k-1][i] :);

IV. Restrictions: up to k transactions;

Idea: With the experience of three questions, the fourth question is ready, but pay attention to the comparison of k and the given array length, because if 2*k>=len, it will be converted to question II. The code is as follows:

int maxProfitiv(int k, vector<int>& prices){
    
    

 int len = prices.size(), K , temp, i, res= 0;

 if(len < 2) return 0;

 if(len<=2*k){
    
    

 for(i = 1; i < len; i++)

 if(prices[i]>prices[i-1])

                    res +=(prices[i]-prices[i-1]);

            return res;

 }

    vector<int> dp(len, 0);

 for(K = 1; K <= k; K++){
    
    

        temp = dp[0] - prices[0];

 for(i = 1; i < len; i++){
    
    

            temp = max(temp, dp[i]-prices[i]);

            dp[i] = max(dp[i-1], prices[i] +temp);

 }

 }

    return dp[len-1];

}

V. Restrictions: each transaction has a transaction fee fee, allowing as many transactions as possible;

Idea: At first I thought that following the idea of ​​question 4, change line 15 to dp[i]= max(dp[i-1], prices[i]+temp-fee); OK, but it’s timed out... Continue the discussion area, look at the big god —> build two arrays hold[i], sold[i], which means that by the i day, the maximum profit of the stock in hand and the maximum profit of selling the stock on the i day, recursion As follows:
hold[i] = max(hold[i-1], sold[i-1]-prices[i]);
sold[i] = max(sold[i-1], hold[i-1]+ prices[i]-fee); The
code is as follows:

int maxProfitiv( vector<int>& prices, int fee){
    
    

 int len = prices.size(), i;

    vector<int> hold(len, 0), sold(len, 0);

    hold[0] = -prices[0];

 for(i = 1; i < len; i++){
    
    

        hold[i] = max(hold[i-1], sold[i-1]-prices[i]);

        sold[i] = max(sold[i-1], hold[i-1]+prices[i]-fee);

 }

    return sold[len-1];

}

VI. Restrictions: After each sale, you must cool down for one day;

Idea: Hey, I don’t know why I have to take a day off, follow the V question line of thought (yes, follow the line of thought...), hold[i] is only related to sold[i-2], the code is modified as follows :

int maxProfitvi(vector<int>& prices){
    
    

 int len = prices.size(), i;

 if(len < 2) return 0;

    vector<int> hold(len, 0), sold(len, 0);

    hold[0] = -prices[0];

    hold[1] = max(hold[0], -prices[1]);

    sold[1] = max(sold[0], hold[1]+prices[1]);

 for(i = 2; i < len; i++){
    
    

        hold[i] = max(hold[i-1], sold[i-2]-prices[i]);

        sold[i] = max(sold[i-1], hold[i-1]+prices[i]);

 }

    return sold[len-1];

}

Summary: Some questions have not changed much, so you can continue to follow your ideas, but once the question changes make your previous general assumptions of the problem-solving ideas no longer hold, you must consider changing your thinking. . . Hey, the road is long and long, I will search up and down~~~ Keep working hard! ! !

Guess you like

Origin blog.csdn.net/lingshengxueyuan/article/details/108580339