leetcode 12 interesting topics in life

Posted by jotgabbi on Sat, 18 Dec 2021 23:17:33 +0100

121. The best time to buy and sell stocks [logic]

Given an array of prices, its ith element prices[i] represents the price of a given stock on day I.

You can only choose to buy this stock one day and sell it on a different day in the future. Design an algorithm to calculate the maximum profit you can make.

Return the maximum profit you can make from this transaction. If you can't make any profit, return 0.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: buy on day 2 (stock price = 1) and sell on day 5 (stock price = 6). Maximum profit = 6-1 = 5.
Note that the profit cannot be 7-1 = 6, because the selling price needs to be greater than the buying price; At the same time, you can't sell stocks before buying.
Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: in this case, no transaction is completed, so the maximum profit is 0.

Tips:

1 <= prices.length <= 105
0 <= prices[i] <= 104

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        
        int min=INT_MAX;
        int ans=0;
        
        for(auto p:prices){
            if(p<min){
                min=p;
            }else{
                ans=max(ans,p-min);
            }
        }
        return ans;
    }
};
  1. At first, I thought it was a stack
  2. Once again, you can only buy and sell once, so just keep the minimum value

122. The best time to buy and sell stocks II [logic, dynamic return, greed]

Give an array prices, where prices[i] is the price of a given stock on day I.

Design an algorithm to calculate the maximum profit you can make. You can complete as many transactions as possible (buying and selling a stock multiple times).

Note: you cannot participate in multiple transactions at the same time (you must sell the previous shares before buying again).

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: if you buy on the second day (stock price = 1) and sell on the third day (stock price = 5), this exchange can make a profit = 5-1 = 4.
Then, buy on the 4th day (stock price = 3) and sell on the 5th day (stock price = 6). This exchange can make a profit = 6-3 = 3.
Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: if you buy on the first day (stock price = 1) and sell on the fifth day (stock price = 5), this exchange can make a profit = 5-1 = 4.
Note that you can't buy stocks on day 1 and day 2 and then sell them. Because you are involved in multiple transactions at the same time, you must sell the previous shares before buying again.
Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: in this case, no transaction is completed, so the maximum profit is 0.

Tips:

1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104

greedy

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minPrice=INT_MAX;
        int maxPrice=INT_MAX;
        int ans=0;
        for(auto p:prices){
            if(p<maxPrice){
                ans+=maxPrice-minPrice;
                minPrice=p;
                maxPrice=p;
            }else{
                maxPrice=p;
            }
            
        }
        ans+=maxPrice-minPrice;
        return ans;
    }
};
  1. At the beginning, I still want to write with stack. It may be related to monotony. I want to use it, but it's not necessary
  2. The difference from the first question lies in multiple times, so just add the range of each increasing sequence

Dynamic regression

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

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

greedy

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

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.
  1. This greed is simpler than I wrote
  2. Because the number of transactions is not limited, if you can collect all the uphill information, you must maximize the benefits

123. The best time to buy and sell stocks III [dynamic return]

Given an array, its ith element is the price of a given stock on day i.

Design an algorithm to calculate the maximum profit you can make. You can complete up to two transactions.

Note: you cannot participate in multiple transactions at the same time (you must sell the previous shares before buying again).

Example 1:

Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: if you buy on the 4th day (stock price = 0) and sell on the 6th day (stock price = 3), this exchange can make a profit = 3-0 = 3.
Then, buy on the 7th day (stock price = 1) and sell on the 8th day (stock price = 4). This exchange can make a profit = 4-1 = 3.
Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: if you buy on the first day (stock price = 1) and sell on the fifth day (stock price = 5), this exchange can make a profit = 5-1 = 4.
Note that you can't buy stocks on day 1 and day 2 and then sell them.
Because you are involved in multiple transactions at the same time, you must sell the previous shares before buying again.
Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: in this case, no transaction is completed, so the maximum profit is 0.
Example 4:

Input: prices = [1]
Output: 0

Tips:

1 <= prices.length <= 105
0 <= prices[i] <= 105

Dynamic regression

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        int buy1,sell1,buy2,sell2;
        buy1=-prices[0];
        sell1=0;
        buy2=-prices[0];
        sell2=0;
        
        for(int i=1;i<n;i++){
            buy1=max(buy1,-prices[i]);
            sell1=max(sell1,buy1+prices[i]);
            buy2=max(buy2,sell1-prices[i]);
            sell2=max(sell2,buy2+prices[i]);
        }

        return max(sell1,sell2);

    }
};
  1. Four states, mutual conversion
  2. Save space
  3. boundary condition

188. The best time to buy and sell stocks IV [dynamic return]

Given an integer array prices, its ith element prices[i] is the price of a given stock on day I.

Design an algorithm to calculate the maximum profit you can make. You can complete up to k transactions.

Note: you cannot participate in multiple transactions at the same time (you must sell the previous shares before buying again).

Example 1:

Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: if you buy on the first day (stock price = 2) and sell on the second day (stock price = 4), this exchange can make a profit = 4-2 = 2.
Example 2:

Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: if you buy on the second day (stock price = 2) and sell on the third day (stock price = 6), this exchange can make a profit = 6-2 = 4.
Then, buy on the 5th day (stock price = 0) and sell on the 6th day (stock price = 3). This exchange can make a profit = 3-0 = 3.

Tips:

0 <= k <= 100
0 <= prices.length <= 1000
0 <= prices[i] <= 1000

Dynamic regression

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {

        if(!k||!prices.size())return 0;

        vector<int>dp(2*k+1,INT_MIN);
        
        dp[0]=-prices[0];
        dp[1]=0;
        
        for(int i=1;i<prices.size();i++){
            for(int j=0;j<2*k;j++){
                if(!j){
                    dp[j]=max(dp[j],-prices[i]);
                }else if(!(j&1)){
                    dp[j]=max(dp[j],dp[j-1]-prices[i]);
                }else{
                    dp[j]=max(dp[j],dp[j-1]+prices[i]);
                }
                //cout<<i<<' '<<j<<' '<<dp[j]<<endl;
            }
        }
        return dp[2*k-1];
    }
};
  1. The reason for the enhanced version of the previous question is the same

Topics: Algorithm leetcode Dynamic Programming greedy algorithm