aboutsummaryrefslogtreecommitdiff
path: root/other/buy_sell_stock.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/buy_sell_stock.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/buy_sell_stock.cpp')
-rw-r--r--other/buy_sell_stock.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/other/buy_sell_stock.cpp b/other/buy_sell_stock.cpp
new file mode 100644
index 0000000..122ee2a
--- /dev/null
+++ b/other/buy_sell_stock.cpp
@@ -0,0 +1,34 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int maxProfit(int k, vector<int> &prices) {
+ if (k == 0)
+ return 0;
+ if (prices.size() == 0 || prices.size() == 1)
+ return 0;
+ vvd dp(k + 1, vector<int>(prices.size()));
+ for (int i = 0; i < dp.size(); i++) {
+ for (int j = 0; j < dp[i].size(); j++) {
+ if (i == 0 || j == 0)
+ dp[i][j] = 0;
+ else {
+ int x, mx = INT_MIN;
+ for (int k = 0; k < j; k++) {
+ x = prices[j] - prices[k] + dp[i - 1][k];
+ mx = max(x, mx);
+ }
+ dp[i][j] = max(dp[i][j - 1], mx);
+ }
+ }
+ }
+ return dp[dp.size() - 1][prices.size() - 1];
+ }
+};
+
+int main() {
+ Solution obj;
+ int k = 2;
+ vector<int> prices = {2, 4, 1};
+ cout << obj.maxProfit(k, prices);
+}