2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Link to the corresponding question:188. The best time to buy and sell stocks IV - LeetCode
Niuke corresponding question link:The best time to buy and sell stocks (Part 4)_Niuketiba_Niuke.com (nowcoder.com)
To make a clearer distinctionBuy it inandSell, we replaceHave stocksandNo stockTwo states.
- f[i][j] means: i After the day, it's done j transactions, at this timeHave stocksMaximum benefit of the state.
- g[i][j] means: i After the day, it's done j transactions, at this timeNo stockMaximum benefit of the state.
for f[i][j]There are also two situations in which i After the day is over, complete j The transaction is now in handHave stocksstatus:
- exist i-1 Day, handHave stocks, and traded j On the i-th day, do nothing.The profit at this time is f[i - 1][j]。
- exist i-1 Day, handNo stocks, and traded j times. i I bought shares on that day.Tickets. Then i At the end of the day, there are stocks. The profit at this time is g[i - 1][j] - prices[i]。
In the above two cases, what we need isMaximum, so the state transfer equation of f is:
f[i][j] = max(f[i - 1][j], g[i - 1][j] - prices[i]
for g[i][j], we have the following two situations in the first i After the day is over, complete j The transaction is now in handNo stocksstatus:
- exist i-1 Days, no stocks in hand, and traded j On the i-th day, do nothing.The profit at this time is g[i - 1][j]。
- exist i-1 Days ago, I had stocks in my hands and traded them. j - 1 times. i When the day comes,The stock is sold. i At the end of the day, we traded. j times. The profit at this time is f[i - 1][j - 1] + prices[i]。
In the above two cases, what we need isMaximum,therefore g The state transfer equation is:
g[i][j] = max(g[i - 1][j], f[i - 1][j - 1] + prices[i])
The transaction relationship between them is as follows:
Fill in each row from top to bottom, and each row from left to right, filling in both tables together.
Returns the maximum value in the sell state, but we don't know how many times it was traded, so we return g The maximum value in the last row of the table.
Our number of transactions will not exceed half of the total number of days, so we can first k Let’s deal with it and optimize the scale of the problem:k = min(k, n / 2)。
- //力扣
- //【动态规划-二维dp-2个状态】
- class Solution {
- //f[i][j]:第i天结束后,完成了j次交易,此时处于“买入”状态下的最大利润
- //g[i][j]:第i天结束后,完成了j次交易,此时处于“卖出”状态下的最大利润
- private:
- const int INF=0x3f3f3f3f;
- public:
- int maxProfit(int k, vector<int>& prices) {
- int n=prices.size();
- k=min(k, n/2); //优化:处理最多交易次数
- vector<vector<int>> f(n, vector<int>(k+1, -INF));
- vector<vector<int>> g(n, vector<int>(k+1, -INF));
- f[0][0]=-prices[0], g[0][0]=0;
- for(int i=1; i<n; i++)
- {
- for(int j=0; j<=k; j++)
- {
- f[i][j]=max(f[i-1][j], g[i-1][j]-prices[i]);
- g[i][j]=g[i-1][j];
- if(j>=1) g[i][j]=max(g[i][j], f[i-1][j-1]+prices[i]);
- }
- }
- int res=0;
- for(int j=0; j<=k; j++)
- res=max(res, g[n-1][j]);
- return res;
- }
- };
-
- //【动态规划-二维dp-2k+1个状态】
- class Solution {
- //dp[i][0] -- 没有操作
- //下面j为奇数:买入;j为偶数:卖出 (j的范围:1~2k-1)
- //dp[i][j] -- 第1~k次买入
- //dp[i][j+1] -- 第1~k次卖出
- public:
- int maxProfit(int k, vector<int>& prices) {
- int n=prices.size();
- vector<vector<int>> dp(n, vector<int>(2*k+1));
- for(int j=1; j<2*k; j+=2)
- dp[0][j]=-prices[0];
- for(int i=1; i<n; i++)
- {
- for(int j=0; j<2*k; j+=2)
- {
- dp[i][j+1]=max(dp[i-1][j+1], dp[i-1][j]-prices[i]);
- dp[i][j+2]=max(dp[i-1][j+2], dp[i-1][j+1]+prices[i]);
- }
- }
- return dp[n-1][2*k];
- }
- };
- //牛客
- #include <iostream>
- #include <cstring>
- using namespace std;
-
- const int INF=0x3f3f3f3f;
- const int N=1010, M=110;
- int prices[N];
- int f[N][M], g[N][M];
- //f[i][j]:第i天结束后,完成了j次交易,此时处于“买入”状态下的最大利润
- //g[i][j]:第i天结束后,完成了j次交易,此时处于“卖出”状态下的最大利润
-
- int main()
- {
- int n, k;
- cin >> n >> k;
- for(int i=0; i<n; i++)
- cin >> prices[i];
- memset(f, -INF, sizeof(f));
- memset(g, -INF, sizeof(g));
- int res=0;
- f[0][0]=-prices[0], g[0][0]=0;
- for(int i=1; i<n; i++)
- {
- for(int j=0; j<=k; j++)
- {
- f[i][j]=max(f[i-1][j], g[i-1][j]-prices[i]);
- g[i][j]=g[i-1][j];
- if(j>0) g[i][j]=max(g[i][j], f[i-1][j-1]+prices[i]);
- res=max(res, g[i][j]);
- }
- }
- cout << res << endl;
- return 0;
- }
-
- //值得学习的代码
- #include <iostream>
- using namespace std;
-
- const int N = 1010, M = 110;
-
- int n, k, p[N];
- int f[N][M], g[N][M];
-
- int main()
- {
- cin >> n >> k;
- for(int i = 0; i < n; i++) cin >> p[i];
-
- k = min(k, n / 2);
- for(int j = 0; j <= k; j++) f[0][j] = g[0][j] = -0x3f3f3f3f;
- f[0][0] = -p[0], g[0][0] = 0;
-
- for(int i = 1; i < n; i++)
- {
- for(int j = 0; j <= k; j++)
- {
- f[i][j] = max(f[i - 1][j], g[i - 1][j] - p[i]);
- g[i][j] = g[i - 1][j];
- if(j >= 1) g[i][j] = max(g[i][j], f[i - 1][j - 1] + p[i]);
- }
- }
-
- int ret = 0;
- for(int j = 0; j <= k; j++) ret = max(ret, g[n - 1][j]);
-
- cout << ret << endl;
-
- return 0;
- }
For a series of questions like buying and selling stocks, you need to thoroughly master the core knowledge points.