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