Day 49 of Code Random Recording Algorithm Training Camp | Topic on Stock Problems (1)

Table of contents

LeeCode 121. Best Time to Buy and Sell Stocks

LeeCode 122. Best Time to Buy and Sell Stocks II


LeeCode 121. Best Time to Buy and Sell Stocks

121. The Best Time to Buy and Sell Stocks - LeetCode

Moving back to five parts:

1. Determine the meaning of the dp array and the subscript: dp[i][0] means the most cash earned from holding stocks on the i-th day; dp[i][1] means the most cash earned from not holding stocks on the i-th day;

2.确定递推公式: dp[i][0] = max(dp[i - 1][0], -prices[i]), dp[i][1] = max(dp[i - 1][1], prices[i] + dp[i - 1][0]);

3. How to initialize the dp array: dp[0][0] -= prices[0], dp[0][1] = 0;

4. Determine the traversal order: traverse from front to back;

5. Example of recursive dp array;

code:

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

Time complexity: O(n) Space complexity: O(n)

Improvement : save space with scrolling array, only record the dp state of the current day and the dp state of the previous day.

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

Time complexity: O(n) Space complexity: O(1)


LeeCode 122. Best Time to Buy and Sell Stocks II

122. The Best Time to Buy and Sell Stocks II - LeetCode

Ideas :

The difference between this question and LeeCode 121. The best time to buy and sell stocks is that a stock can be bought and sold multiple times, so when you buy a stock on the i-th day, the cash you hold may have the profit from previous transactions.

Moving back to five parts:

1. Determine the meaning of the dp array and the subscript: dp[i][0] means the most cash earned from holding stocks on the i-th day; dp[i][1] means the most cash earned from not holding stocks on the i-th day;

2.确定递推公式: dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]), dp[i][1] = max(dp[i - 1][1], prices[i] + dp[i - 1][0]);

3. How to initialize the dp array: dp[0][0] -= prices[0], dp[0][1] = 0;

4. Determine the traversal order: traverse from front to back;

5. Example of recursive dp array;

code:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
    	int len = prices.size();
    	vector<vector<int>> dp(len, vector<int>(2, 0));
    	dp[0][0] -= prices[0];
    	dp[0][1] = 0;
    	for (int i = 1; i < len; 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[len - 1][1];
    }
};

Time complexity: O(n) Space complexity: O(n)

Improvement : save space with scrolling array, only record the dp state of the current day and the dp state of the previous day.

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

Time complexity: O(n) Space complexity: O(1)

Guess you like

Origin blog.csdn.net/weixin_74976519/article/details/131133964