-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimal_Strategy_Game-DP.cpp
More file actions
60 lines (52 loc) · 1.47 KB
/
Copy pathOptimal_Strategy_Game-DP.cpp
File metadata and controls
60 lines (52 loc) · 1.47 KB
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
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
/*
Time Complexity : O(n^2)
Space Complexity : O(n*n)
*/
int optimalGame(int a[] , int n)
{
int dp[n][n];
for(int gap=0 ; gap<n ; gap++)
{
for(int i=0, j=gap ; j<n ; i++, j++)
{
//we have only one option to choose
if(gap == 0)
dp[i][j] = a[i];
//will choose maximum of the a[i] and a[j]
else if(gap == 1)
{
dp[i][j] = max(a[i] , a[j]);
}
//for the cases in which number of nodes is greater than 3
else
{
int val1 = a[i] + min(dp[i+2][j] , dp[i+1][j-1]);
//choice is : i ----> i+1 , j
//------------------------------
//either i+1 --> i+2 , j
//or j --> i+1 , j-1
int val2 = a[j] + min(dp[i+1][j-1] , dp[i][j-2]);
//choice is : j ----> i , j-1
//-------------------------------
//either i --> i+1 , j-1
//or j-1 --> i , j-2
dp[i][j] = max(val1 , val2);
}
}
}
return dp[0][n-1];
}
int main()
{
int n;
cout<<"Enter the size of the array : ";
cin>>n;
int a[n];
cout<<"Enter the values :\n";
for(int i=0 ; i<n ; i++)
cin>>a[i];
cout<<"Maximum possible choice that can be made is : "<<optimalGame(a , n)<<endl;
return 0;
}