Skip to content

Commit ad75763

Browse files
Create PotsOfGold.cpp
1 parent 546823f commit ad75763

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: PotsOfGold.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Refer to the complete problem statement here: https://practice.geeksforgeeks.org/problems/pots-of-gold-game/1
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int maxCoins(int A[],int );
7+
8+
int main() {
9+
int t;
10+
cin>>t;
11+
while(t--)
12+
{
13+
int n;
14+
cin>>n;
15+
int a[n];
16+
for(int i=0;i<n;i++)
17+
{
18+
cin>>a[i];
19+
}
20+
cout<<maxCoins(a,n)<<endl;
21+
}
22+
return 0;
23+
}
24+
25+
int maxCoins(int A[],int N)
26+
{
27+
int n=N;
28+
int dp[N][N];
29+
memset(dp,0,sizeof(dp));
30+
for(int gap=0;gap<n;gap++){
31+
for(int i=0,j=i+gap;i<n && j<n;i++,j++){
32+
if(gap==0)
33+
dp[i][j]=A[i];
34+
else if(gap==1)
35+
dp[i][j] = max(A[i],A[j]);
36+
else{
37+
dp[i][j] = max(A[i]+min(dp[i+2][j],dp[i+1][j-1]),
38+
min(dp[i][j-2],dp[i+1][j-1])+A[j]);
39+
}
40+
}
41+
42+
}
43+
return dp[0][n-1];
44+
}

0 commit comments

Comments
 (0)