Optimal Strategy For A Game | Practice | GeeksforGeeks

You are given an array A of size N. The array contains integers and is of even length. The elements of the array represent N coin of values V1, V2, ....Vn. You play against an opponent in an alternating way. 
In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin.
You need to determine the maximum possible amount of money you can win if you go first.
Note: Both the players are playing optimally.
Example 1:
Input:
N = 4
A[] = {5,3,7,10}
Output: 15
Explanation: The user collects maximum
value as 15(10 + 5)

Example 2:
Input:
N = 4
A[] = {8,15,3,7}
Output: 22
Explanation: The user collects maximum
value as 22(7 + 15)
Your Task:
Complete the function maximumAmount() which takes an array arr[] (represent values of N coins) and N as number of coins as a parameter and returns the maximum possible amount of money you can win if you go first.
Expected Time Complexity : O(N*N)
Expected Auxiliary Space: O(N*N)
Constraints:
2 <= N <= 103
1 <= Ai <= 106
0
milindprajapatmst195 days ago
Total Time Taken: 0.33/1.14
Time Complexity: O(n*n)
Space Complexity: O(n*n)
 
# define ll long long
# define pi pair<ll, ll>
# define mp make_pair
# define f first
# define s second

const int N = 1e3;
pi dp[N][N];

class Solution {
    public:
    pi help(int k1, int k2, int arr[]) {
        if (k1 == k2)
            return mp(arr[k1], 0);
        if (dp[k1][k2].f == -1) {
            pi p1 = help(k1 + 1, k2, arr);
            pi p2 = help(k1, k2 - 1, arr);
            if (p1.s + arr[k1] < p2.s + arr[k2])
                dp[k1][k2] = mp(p2.s + arr[k2], p2.f);
            else
                dp[k1][k2] = mp(p1.s + arr[k1], p1.f);
        }
        return dp[k1][k2];
    }
    ll maximumAmount(int arr[], int n) {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                dp[i][j] = mp(-1, -1);
        pi p = help(0, n - 1, arr);
        return p.f;
    }
};
0
maheshahirwar2042 weeks ago
Here is amazing java solution
 
class solve{   //Function to find the maximum possible amount of money we can win.   static long[][]dp=new long[1001][1001];   static long countMaximum(int arr[], int n)   {       for(long[]a:dp)Arrays.fill(a,-1);      return solve(arr,0,n-1);   }static long solve(int[]arr,int l,int r){   if(l+1==r){       return dp[l][r]=Math.max(arr[l],arr[r]);   }   if(dp[l][r]!=-1)return dp[l][r];   long tem1,tem2,tem3,tem4;   long first,second;   if(dp[l+2][r]!=-1){        tem1=dp[l+2][r];   }else{       tem1=solve(arr,l+2,r);       dp[l+2][r]=tem1;   }   if(dp[l+1][r-1]!=-1){       tem3=tem2=dp[l+1][r-1];   }else{       tem3=tem2=solve(arr,l+1,r-1);       dp[l+1][r-1]=tem2;   }    if(dp[l][r-2]!=-1){        tem4=dp[l][r-2];    }else{        tem4=solve(arr,l,r-2);        dp[l][r-2]=tem4;    }    first=arr[l]+Math.min(tem1,tem2);    second=arr[r]+Math.min(tem3,tem4);   return dp[l][r]=Math.max(first,second);}    } 
0
ashutosh0920003 weeks ago
public class OptimalStrategyForAGame {public static int  optimalStrategy(int[] arr) {int[][] dp = new int[arr.length][arr.length];//g=>gap (we use gap algorithm here)// like count palindromic subsequencesfor(int g=0;g<dp.length;g++) { for(int i=0,j=g;j<dp.length;i++,j++) {  if(g==0) {   //gap => 0    dp[i][j] = arr[i];  }  else if(g==1) {   dp[i][j] = Math.max(arr[i], arr[j]);  }  else {   int val1 = arr[i] + Math.min(dp[i+2][j],dp[i+1][j-1]); // ye mere sath ho rha hai to iss liye maine min manba hai    //i =>i+1 , j //I take i      // if i+1 -> i+2,j   // or j -> i+1 , j-1   // agar hum i lete hai tab j opponent ke liye rhega aur agar hum j lete hai ti i    //opponent ke liye rhega   int val2 = arr[j] + Math.min(dp[i+1][j-1], dp[i][j-2]); //ye mere sath ho rha hai isliye min mantre hai   // j -> i , j-1  // I take j      //if j-1 ->i , j-2   //or i+1 -> i+1 , j-1   //similarly phle hume j le lia tha      int val = Math.max(val1, val2); // isme hme khudh krna hai isliye hum max mante hai   dp[i][j] = val;  } }}return dp[0][arr.length-1];//print top right}public static void main(String[] args) {int[] arr = {20,30,2,10};System.out.println(optimalStrategy(arr));}}
 
0
ashutosh092000
This comment was deleted.
0
ruchitchudasama1232 months ago
public:
    
    long long go(int arr[],int l,int h,vector<vector<long long>> &dp){
        if(l+1>h){
            return 0;
        }
        if(dp[l][h]!=-1)return dp[l][h];
        
        long long first=arr[l]+min(go(arr,l+2,h,dp),go(arr,l+1,h-1,dp));
        long long second=arr[h]+min(go(arr,l,h-2,dp),go(arr,l+1,h-1,dp));
        return dp[l][h]=max(first,second);
    }
    long long maximumAmount(int arr[], int n){
        vector<vector<long long>> dp(n+1,vector<long long>(n+1,-1));
        return go(arr,0,n-1,dp);
    }
+1
aloksinghbais022 months ago
C++ solution using memoization having time complexity as O(N*N) and space complexity as O(N*N) is as follows :- 
 
Execution Time :- 0.1 / 1.1 sec
 
long long int dp[1001][1001];      long long int max(long long int a,long long int b){       return a > b ? a : b;   }      long long int helper(int arr[],int i,int j){       if(i == j){           return arr[i];       }       if(i > j) return (0);              if(dp[i][j] != -1) return (dp[i][j]);              long long int res1 = arr[j] + min(helper(arr,i,j-2),helper(arr,i+1,j-1));       long long int res2 = arr[i] + min(helper(arr,i+2,j),helper(arr,i+1,j-1));       return dp[i][j] = max(res1,res2);   }      long long maximumAmount(int arr[], int n){       memset(dp,-1,sizeof(dp));       return helper(arr,0,n-1);   }
-1
vishalpandey100220002 months ago
static long countMaximum(int arr[], int n)   {       // Your code here       long dp[][]=new long[n][n];       for (int i=0;i<n-1;i++){           dp[i][i+1]=Math.max(arr[i],arr[i+1]);       }       for (int gap=3;gap<n;gap=gap+2){           for (int i = 0; i+gap<n; i++){               int j=i+gap;               dp[i][j]=Math.max(arr[i]+Math.min(dp[i+2][j],dp[i+1][j-1]),arr[j]+Math.min(dp[i+1][j-1],dp[i][j-2]));           }       }       return dp[0][n-1];   }
0
victor19108662 months ago
Memoization Approach Easy to Understand || c++
 
long long int helper(int arr[],int i,int n,vector<vector<long long int>> &dp){       if(i>n)return 0;       if(i==n)return arr[i];       if(dp[i][n]!=-1)return dp[i][n];       return dp[i][n]=max(arr[i]+min(helper(arr,i+1,n-1,dp),helper(arr,i+2,n,dp)),       arr[n]+min(helper(arr,i+1,n-1,dp),helper(arr,i,n-2,dp)));   }   long long maximumAmount(int arr[], int n){       vector<vector<long long int>> dp(n+1,vector<long long int>(n+1,-1));       return helper(arr,0,n-1,dp);   }
0
jyotish04
This comment was deleted.
0
amishasahu3283 months ago
class Solution{
    public:
    // Memoization method
    long long dp[1001][1001];
    long long solve(int arr[], int n, int i, int j)
    {
        // below diagonal values zero
        if(i > j)
            dp[i][j] = 0;
        // filling the central diagonal value
        if(i == j)
            dp[i][j] = arr[i];
        
        if(dp[i][j] != -1)
            return dp[i][j];
            
        long long int front_pick = arr[i]+ min(solve(arr,n,i+2,j),solve(arr,n,i+1,j-1));
        long long int back_pick = arr[j]+ min(solve(arr,n,i,j-2),solve(arr,n,i+1,j-1));
        
        return dp[i][j] = max(front_pick, back_pick);
        
    }
    long long maximumAmount(int arr[], int n){
        memset(dp, -1, sizeof(dp));
        return solve(arr, n, 0, n-1);
        
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.