Skip to content

Commit 7ae4a98

Browse files
author
wb-hjk570755
committed
预测赢家
1 parent 0d34cee commit 7ae4a98

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

src/com/blankj/myself/ArrHeaderTailMax.java

+28-10
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,46 @@ public static int max(int[] a){
1717
}
1818
System.out.println("sum="+sum);
1919

20-
return maxDetail(a,sum,0,a.length-1,0);
20+
//return maxDetail(a,sum,0,a.length-1,0);
21+
22+
return maxDetail(a,0,a.length-1,0);
23+
2124
}
2225

23-
public static int maxDetail(int[] a,int sum,int leftIndex,int rightIndex,int max){
24-
int chooseNum = 0;
25-
if(rightIndex-leftIndex>1){
26-
int leftMax = sum - maxDetail(a,sum-a[leftIndex],leftIndex+1,rightIndex,max);
27-
int rightMax = sum - maxDetail(a,sum-a[rightIndex],leftIndex,rightIndex-1,max);
26+
//public static int maxDetail(int[] a,int sum,int leftIndex,int rightIndex,int max){
27+
// if(rightIndex-leftIndex>1){
28+
// int leftMax = sum - maxDetail(a,sum-a[leftIndex],leftIndex+1,rightIndex,max);
29+
// int rightMax = sum - maxDetail(a,sum-a[rightIndex],leftIndex,rightIndex-1,max);
30+
//
31+
// max = max + Math.max(leftMax,rightMax);
32+
// System.out.println("leftMax="+leftMax+", rightMax="+rightMax+", Math.max(leftMax,rightMax)="+Math.max(leftMax,rightMax));
33+
// System.out.println("max = " + max);
34+
// return max;
35+
// }else {
36+
// System.out.println("leftMax="+a[leftIndex]+", rightMax="+a[rightIndex]+", Math.max(leftMax,rightMax)==== "+Math.max(a[leftIndex],a[rightIndex]));
37+
// return Math.max(a[leftIndex],a[rightIndex]);
38+
// }
39+
//}
2840

29-
chooseNum = Math.max(leftMax,rightMax);
30-
return chooseNum;
41+
public static int maxDetail(int[] a,int leftIndex,int rightIndex,int max){
42+
if(rightIndex-leftIndex>1){
43+
int chooseNum = maxDetail(a,leftIndex+1,rightIndex,max)>maxDetail(a,leftIndex,rightIndex-1,max)?
44+
a[rightIndex]:a[leftIndex];
45+
max = max + chooseNum;
46+
return max;
3147
}else {
3248
return Math.max(a[leftIndex],a[rightIndex]);
3349
}
3450
}
3551

3652
public static void main(String[] args) {
37-
int v[] = { 1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2 };
53+
//int v[] = { 1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2 };
54+
int v[] = { 10, 70,3,2,8,900};
55+
3856
int n = max(v);
3957
System.out.println(n);
4058

41-
System.out.println(max2_dync(v));
59+
//System.out.println(max2_dync(v));
4260
}
4361

4462
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/10/1
10+
*/
11+
public class PredictTheWinner {
12+
public static void main(String[] args) {
13+
int v[] = { 1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2 };
14+
PredictTheWinner predictTheWinner = new PredictTheWinner();
15+
System.out.println(predictTheWinner.PredictTheWinner(v));
16+
}
17+
18+
public boolean PredictTheWinner(int[] nums) {
19+
if(nums.length%2==0){
20+
return true;
21+
}
22+
return predictTheWinnerReal(nums,0,nums.length-1)>=0;
23+
}
24+
25+
public int predictTheWinnerReal(int[] nums,int l,int r){
26+
if(l==r){
27+
return nums[l];
28+
}
29+
int left = nums[l] - predictTheWinnerReal(nums,l+1,r);
30+
int right = nums[r] - predictTheWinnerReal(nums,l,r-1);
31+
32+
return Math.max(left,right);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)