From 6567b4834a594a0e56923b617998513168c7a519 Mon Sep 17 00:00:00 2001 From: imp Date: Sun, 5 Dec 2021 14:09:03 +0800 Subject: [PATCH] feat: add cpp solution to lc problem: No.188 --- .../README.md | 35 +++++++++++++++--- .../README_EN.md | 36 ++++++++++++++++--- .../Solution.cpp | 17 +++++++++ 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/Solution.cpp diff --git a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README.md b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README.md index 758c821f27ae0..81e248c394058 100644 --- a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README.md +++ b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README.md @@ -44,6 +44,17 @@ +动态规划 + +**dp[i][0]** 表示第 *i* 次买入后的收益, + +**dp[i][1]** 表示第 *i* 次卖出后的收益。 + +状态转移方程: + +**dp[i][0] = max{dp[i][0], dp[i - 1][1] + prices[i]}** + +**dp[i][1] = max{dp[i][1], dp[i][0] + prices[i]}** ### **Python3** @@ -62,10 +73,26 @@ ``` -### **...** - -``` - +### **C++** + +```cpp +class Solution { +public: + int maxProfit(int k, vector& prices) { + int dp[k + 1][2]; + memset(dp, 0, sizeof(dp)); + for(int i = 1; i <= k && !prices.empty(); ++i) { + dp[i][0] = -prices[0]; + } + for(int i = 1; i < prices.size(); ++i) { + for(int j = 1; j <= k; ++j) { + dp[j][0] = max(dp[j][0], dp[j - 1][1] - prices[i]); + dp[j][1] = max(dp[j][1], dp[j][0] + prices[i]); + } + } + return dp[k][1]; + } +}; ``` diff --git a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md index fbe242f8b18bc..4413f534bf123 100644 --- a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md +++ b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md @@ -39,6 +39,18 @@ ## Solutions +Dynamic programming + +**dp[i][0]** Indicates the income after the *ith* purchase, + +**dp[i][1]** Indicates the income after the *ith* sell. + +State transition equation: + +**dp[i][0] = max{dp[i][0], dp[i - 1][1] + prices[i]}** + +**dp[i][1] = max{dp[i][1], dp[i][0] + prices[i]}** + ### **Python3** @@ -53,10 +65,26 @@ ``` -### **...** - -``` - +### **C++** + +```cpp +class Solution { +public: + int maxProfit(int k, vector& prices) { + int dp[k + 1][2]; + memset(dp, 0, sizeof(dp)); + for(int i = 1; i <= k && !prices.empty(); ++i) { + dp[i][0] = -prices[0]; + } + for(int i = 1; i < prices.size(); ++i) { + for(int j = 1; j <= k; ++j) { + dp[j][0] = max(dp[j][0], dp[j - 1][1] - prices[i]); + dp[j][1] = max(dp[j][1], dp[j][0] + prices[i]); + } + } + return dp[k][1]; + } +}; ``` diff --git a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/Solution.cpp b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/Solution.cpp new file mode 100644 index 0000000000000..1a68ed3a96c3d --- /dev/null +++ b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxProfit(int k, vector& prices) { + int dp[k + 1][2]; + memset(dp, 0, sizeof(dp)); + for(int i = 1; i <= k && !prices.empty(); ++i) { + dp[i][0] = -prices[0]; + } + for(int i = 1; i < prices.size(); ++i) { + for(int j = 1; j <= k; ++j) { + dp[j][0] = max(dp[j][0], dp[j - 1][1] - prices[i]); + dp[j][1] = max(dp[j][1], dp[j][0] + prices[i]); + } + } + return dp[k][1]; + } +}; \ No newline at end of file