-
Notifications
You must be signed in to change notification settings - Fork 40
/
11 August "Problem of the Day" Answer
55 lines (45 loc) · 1.23 KB
/
11 August "Problem of the Day" Answer
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
45
46
47
48
49
50
51
52
53
54
55
GeeksforGeeks
The Question is :- "Pots of Gold Game"
Answer :-
class Solution {
public:
// int solve(vector<int>&nums,int i, int j){
// if(i==j)return nums[i];
// if(i>j)return 0;
// int left = nums[i]+min(solve(nums,i+2,j),solve(nums,i+1,j-1));
// int right = nums[i]+min(solve(nums,i,j-2),solve(nums,i+1,j-1));
// return max(left,right);
// }
// int maxCoins(vector<int>&nums,int n)
// {
// //Write your code here
// return solve(nums,0,n-1);
// }
int dp[1001][1001];
int solve(vector<int>& A,int i,int j)
{
if(i==j)
{
return A[i];
}
if(i>j)
{
return 0;
}
if(dp[i][j]!=-1)
{
return dp[i][j];
}
int left = A[i] + min(solve(A,i+2,j),solve(A,i+1,j-1));
int right = A[j] + min(solve(A,i,j-2),solve(A,i+1,j-1));
return dp[i][j] = max(left,right);
}
int maxCoins(vector<int>&A,int n)
{
memset(dp,-1,sizeof(dp));
return solve(A,0,n-1);
}
};
Hope you understand the answer, and complete it.
Stay Connected for daily Problem of the Day answers.
Thank you All!