Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added best time to buy sell stock IV in leetcode folder #520

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Question Link:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.

Find the maximum profit you can achieve. You may complete at most k transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
*/
/*
Input: k = 2, prices = [2,4,1]
Output: 2
*/

class Solution {
public:
int dp[1001][2][101];
int solve(int i,int own,int k,vector<int>& prices)
{
// if i becomes greater than prices array length then there are no more stocks left . hence we return.
if(i>=prices.size())
{
return 0;
}
// if the number of transactions performed become greater than k then also we return since you can perform max k transactions.
if(k<=0)
{
return 0;
}
// memoize ... check if the situation already exist then we just take that value.
if(dp[i][own][k]!=-1)
{
return dp[i][own][k];
}
// if you own a stock at that paticular day then you two options either to sell it or dont sell that stock.
// we call solve function for both the options.
if(own)
{
// if we decide to sell the stock then we add the price of stock at that day
// then we go to next day that is increment i , and make variable own to 0 , and reduce the number of transaction available to us by 1
int opt1=prices[i]+solve(i+1,0,k-1,prices);

// if we do not sell the stock then
// we go to next day that is increment i , do not change the own variable cause we still own the stock , and pass k unchanged
int opt2=solve(i+1,1,k,prices);

// we store the max of the two in the dp array cause we have to maximize the profit
dp[i][1][k]=max(opt1,opt2);
// hence we return this value.
return dp[i][1][k];
}
// if you do not own a stock at that paticular day then you two options either to buy the stock on that day or dont buy.
// we call solve function for both the options.
else
{
// if we decide to buy the stock then we subtract the price of stock at that day
// then we go to next day that is increment i , and make variable own to 1 , and pass k unchanged
int opt1=-(prices[i])+solve(i+1,1,k,prices);

// if we do not buy the stock then
// we go to next day that is increment i , do not change the own variable cause we still dont own the stock , and pass k unchanged
int opt2=solve(i+1,0,k,prices);

// we store the max of the two in the dp array cause we have to maximize the profit
dp[i][0][k]=max(opt1,opt2);
// hence we return this value.
return dp[i][0][k];
}
}
int maxProfit(int k,vector<int>& prices) {
// set the dp array to -1 initially
memset(dp,-1,sizeof(dp));

// call the solve function
// pass 0 in i cause we start with the 0th element in the prices array
// then we pass 0 in own variable cause we do not own a stock at the starting
return solve(0,0,k,prices);
}
};