A line of cards game problem public class cardproblem{//Violent Recursive method (Time complexity O (2^n), Space complexity O (n)) public static int Win01 (Int[]arr) {if (arr ==null| |
arr.length==0) {return 0;
Return Math.max (f (arr,0,arr.length-1), S (arr,0,arr.length-1));
}///First selected optimal function public static int F (Int[]arr,int I,int j) {if (i==j) {return arr[i];
Return Math.max (Arr[i]+s (arr,i+1,j), Arr[j]+s (arr,i,j-1));
}//later optimal function public static int s (Int[]arr,int I,int j) {if (i==j) {return 0;
Return Math.min (f (arr,i+1,j), F (arr,i,j-1)); //*******************************************************************************//Dynamic programming method (Time complexity O (n^2), Space complexity O (n ^2)) public static int Win02 (Int[]arr) {if (arr==null| |
arr.length==0) {return 0; }//Generate dynamic Programming matrix Int[][]f=new int[arr.length][arr.length];//first-choice optimal function matrix Int[][]s=new int[arr.length][arr.length];//optimal Letter Number matrix for (int j=0;j<arr.length;j++) {f[j][j]=arr[j];//diagonal position value for (int i=j-1;i>=0;i--) {F[i][j]=mat H.max (Arr[i]+s[i+1][j],Arr[j]+s[i][j-1]);
S[i][j]=math.min (F[i+1][j],f[i][j-1]);
} return Math.max (F[0][arr.length-1],s[0][arr.length-1]);
public static void Main (String[]args) {int[]arr={1,2,100,4}; System.out.println (Win01 (arr))//Violent recursion System.out.println (WIN02 (arr));//Dynamic Planning}}