-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictTheWinner.java
More file actions
31 lines (28 loc) · 1.07 KB
/
PredictTheWinner.java
File metadata and controls
31 lines (28 loc) · 1.07 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
/**
* Created by yang on 17-8-26.
*/
public class PredictTheWinner {
public boolean PredictTheWinner(int[] nums) {
if (nums == null || nums.length == 0) {
return true;
}
int n = nums.length;
// advantage[i][j] means "the advantage the first player will get when choose from nums[i...to...j]"
int[][] advantage = new int[n][n];
for (int i = 0; i < n; i++) {
advantage[i][i] = nums[i];
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i <= n-len; i++) {
int j = i+len-1;
advantage[i][j] = Math.max(nums[i] - advantage[i+1][j], nums[j] - advantage[i][j-1]);
}
}
return advantage[0][n-1] >= 0;
}
public static void main(String[] args) {
PredictTheWinner predictTheWinner = new PredictTheWinner();
System.out.println(predictTheWinner.PredictTheWinner(new int[]{1, 5, 2}) + " <---> false");
System.out.println(predictTheWinner.PredictTheWinner(new int[]{1, 5, 233, 7}) + " <---> true");
}
}