A line of Cards game problem __ A line of cards game problem

Source: Internet
Author: User

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}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.