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

Created a New algorithm for Wines Problem #334

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions algorithms/dynamic-programming/Wines Problem Top down.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<bits/stdc++.h>
using namespace std;
int max_profit(int n,int prices[],int i,int j,int y,int dp[100][100]){
//base case
if(i>j){
return 0;
}
if(dp[i][j]!=0){
return dp[i][j];
}
//recursive case
int op1,op2;
op1=prices[i]*y+max_profit(n,prices,i+1,j,y+1,dp);
op2=prices[j]*y+max_profit(n,prices,i,j-1,y+1,dp);
int ans=max(op1,op2);
return dp[i][j]=ans;
}
int main(){
int n;
cin>>n;
int prices[n];
for(int i=0;i<n;i++){
cin>>prices[i];
}
int dp[100][100]={0};
cout<<max_profit(n,prices,0,n-1,1,dp);
}