From 2d3240b5f4f967026ad8841bc0a69764b17bb1c1 Mon Sep 17 00:00:00 2001 From: Aditi Agarwal Date: Mon, 20 Oct 2025 07:35:10 +0530 Subject: [PATCH 1/2] Solution for Best time to buy and sell stock --- 123.Best Time to Buy and Sell Stock III.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 123.Best Time to Buy and Sell Stock III.cpp diff --git a/123.Best Time to Buy and Sell Stock III.cpp b/123.Best Time to Buy and Sell Stock III.cpp new file mode 100644 index 0000000..ee7e12a --- /dev/null +++ b/123.Best Time to Buy and Sell Stock III.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int buy1 = -prices[0]; + int sell1 = 0; + int buy2 = -prices[0]; + int sell2 = 0; + + for (int i = 1; i < prices.size(); i++) { + int price = prices[i]; + buy1 = max(buy1, -price); + sell1 = max(sell1, buy1 + price); + buy2 = max(buy2, sell1 - price); + sell2 = max(sell2, buy2 + price); + } + + return sell2; + } +}; \ No newline at end of file From ef421cbce3250cf9b8606ca4c2004e7f1bcbdab2 Mon Sep 17 00:00:00 2001 From: Aditi Agarwal Date: Mon, 20 Oct 2025 07:40:16 +0530 Subject: [PATCH 2/2] Soln for Best time to buy and sell stock V --- 123.Best Time to Buy and Sell Stock III.cpp | 19 ----------- 3573.Best Time to Buy and Sell Stock V.cpp | 37 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 19 deletions(-) delete mode 100644 123.Best Time to Buy and Sell Stock III.cpp create mode 100644 3573.Best Time to Buy and Sell Stock V.cpp diff --git a/123.Best Time to Buy and Sell Stock III.cpp b/123.Best Time to Buy and Sell Stock III.cpp deleted file mode 100644 index ee7e12a..0000000 --- a/123.Best Time to Buy and Sell Stock III.cpp +++ /dev/null @@ -1,19 +0,0 @@ -class Solution { -public: - int maxProfit(vector& prices) { - int buy1 = -prices[0]; - int sell1 = 0; - int buy2 = -prices[0]; - int sell2 = 0; - - for (int i = 1; i < prices.size(); i++) { - int price = prices[i]; - buy1 = max(buy1, -price); - sell1 = max(sell1, buy1 + price); - buy2 = max(buy2, sell1 - price); - sell2 = max(sell2, buy2 + price); - } - - return sell2; - } -}; \ No newline at end of file diff --git a/3573.Best Time to Buy and Sell Stock V.cpp b/3573.Best Time to Buy and Sell Stock V.cpp new file mode 100644 index 0000000..87bba48 --- /dev/null +++ b/3573.Best Time to Buy and Sell Stock V.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + vector prices; + long long mn = -1e14; + vector>> dp; + long long f(int i, int k, int state){ + if(i == prices.size()){ + return state == 0 ? 0 : mn; + } + if(dp[i][k][state] != mn) return dp[i][k][state]; + + long long p = prices[i]; + long long profit = mn; + profit = max(profit, f(i + 1, k, state)); + + if(state == 0){ + profit = max(profit, f(i+1, k, 1) - p); + profit = max(profit, f(i+1, k, 2) + p); + } + else if(k > 0){ // have transactions left + if(state == 1){ + profit = max(profit, f(i+1, k-1, 0) + p); + + } + else{ + profit = max(profit, f(i+1, k-1, 0) - p); + } + } + return dp[i][k][state] = profit; + } + + long long maximumProfit(vector& prices, int k) { + this->prices = prices; + dp.assign(prices.size() + 1, vector> (k + 1, vector (3, mn))); + return f(0, k, 0); + } +}; \ No newline at end of file