Created
April 5, 2020 14:49
-
-
Save SuryaPratapK/a32c0a7f02ecb11c371809a4ec2a7b3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PEAK_VALLEY APPROACH | |
class Solution { | |
public: | |
int maxProfit(vector<int>& prices) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
int n = prices.size(); | |
int diff = 0; | |
for(int i=1;i<n;++i) | |
{ | |
if(prices[i] > prices[i-1]) | |
diff += prices[i]-prices[i-1]; | |
} | |
return diff; | |
} | |
}; | |
//LOOKUP_TABLE | |
class Solution { | |
int find_maxProfit(bool buy,int pos,vector<int> prices,int n,vector<int> &dp_buy,vector<int> &dp_sell) | |
{ | |
if(pos>=n || (buy==true && pos==n-1)) | |
return 0; | |
else if(buy && dp_buy[pos]>0) | |
return dp_buy[pos]; | |
else if(!buy && dp_sell[pos]>0) | |
return dp_sell[pos]; | |
int profit = 0; | |
//We have 3 options: Buy,Sell,Skip (current share) | |
int skip = find_maxProfit(buy,pos+1,prices,n,dp_buy,dp_sell); //Skip case | |
if(buy) | |
{ | |
profit = -prices[pos] + find_maxProfit(false,pos+1,prices,n,dp_buy,dp_sell); | |
dp_buy[pos] = max(skip,profit); | |
return dp_buy[pos]; | |
} | |
else | |
{ | |
profit = prices[pos] + find_maxProfit(true,pos+1,prices,n,dp_buy,dp_sell); | |
dp_sell[pos] = max(skip,profit); | |
return dp_sell[pos]; | |
} | |
//Return by taking max of all 3 cases | |
return max(profit,skip); | |
} | |
public: | |
int maxProfit(vector<int>& prices) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
int n = prices.size(); | |
if(n==0 || n==1) | |
return 0; | |
else if(n==2) | |
return max(0,prices[1]-prices[0]); | |
vector<int> dp_buy(n,0); | |
vector<int> dp_sell(n,0); | |
//vector<int> dp(n,0); | |
return find_maxProfit(true,0,prices,n,dp_buy,dp_sell); | |
} | |
}; |
yes ,pls explain dp solution
@Pawanupadhyay10 see my approach but it will give tle in last test case
class Solution {
int dp[30005][2];
int find_maxProfit(bool buy,int pos,vector prices,int n)
{
if(pos>=n || (buy==true && pos==n-1))
return 0;
if(dp[pos][buy]!=-1)
return dp[pos][buy];
int profit = 0;
int skip = find_maxProfit(buy,pos+1,prices,n); //Skip case
if(buy)
{
profit = -prices[pos] + find_maxProfit(false,pos+1,prices,n);
}
else
{
profit = prices[pos] + find_maxProfit(true,pos+1,prices,n);
}
return dp[pos][buy]= max(profit,skip);
}
public:
int maxProfit(vector& prices) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n = prices.size();
if(n==0 || n==1)
return 0;
else if(n==2)
return max(0,prices[1]-prices[0]);
memset(dp,-1,sizeof(dp));
return find_maxProfit(true,0,prices,n);
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you explain the intuition behind this lookup table it's hard to understand the recursion here and why did you take two vectors dp_buy,dp_sell and what are you doing with them