Coins in a Line
here are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.
Could you please decide the first play will win or lose?
博弈累问题, 隔一层搜索 Example
n = 1, return true.
n = 2, return true.
n = 3, return false.
n = 4, return true.
n = 5, return true.
public class Solution {
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int n) {
// write your code here
int []dp = new int[n+1];
return MemorySearch(n, dp);
}
boolean MemorySearch(int n, int []dp) { // 0 is empty, 1 is false, 2 is true
if(dp[n] != 0) {
if(dp[n] == 1)
return false;
else
return true;
}
if(n <= 0) {
dp[n] = 1;
} else if(n == 1) {
dp[n] = 2;
} else if(n == 2) {
dp[n] = 2;
} else if(n == 3) {
dp[n] = 1;
} else {
if((MemorySearch(n-2, dp) && MemorySearch(n-3, dp)) ||
(MemorySearch(n-3, dp) && MemorySearch(n-4, dp) )) {
dp[n] = 2;
} else {
dp[n] = 1;
}
}
if(dp[n] == 2)
return true;
return false;
}
}
Coins in a line II
There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.
Could you please decide the first player will win or lose?
public class Solution {
/**
* @param values: an array of integers
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int[] values) {
// write your code here
int []dp = new int[values.length + 1];
boolean []flag =new boolean[values.length + 1];
int sum = 0;
for(int now : values)
sum += now;
return sum < 2*MemorySearch(values.length, dp, flag, values);
}
int MemorySearch(int n, int []dp, boolean []flag, int []values) {
if(flag[n] == true)
return dp[n];
flag[n] = true;
if(n == 0) {
dp[n] = 0;
} else if(n == 1) {
dp[n] = values[values.length-1];
} else if(n == 2) {
dp[n] = values[values.length-1] + values[values.length-2];
} else if(n == 3){
dp[n] = values[values.length-2] + values[values.length-3];
} else {
dp[n] = Math.max(
Math.min(MemorySearch(n-2, dp, flag,values) , MemorySearch(n-3, dp, flag, values)) + values[values.length-n],
Math.min(MemorySearch(n-3, dp, flag, values), MemorySearch(n-4, dp, flag, values)) + values[values.length-n] + values[values.length - n + 1]
);
}
return dp[n];
}
}
coins in a line III
There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.
Could you please decide the first player will win or lose?
public boolean firstWillWin(int[] values) {
// write your code here
int n = values.length;
int [][]dp = new int[n + 1][n + 1];
boolean [][]flag =new boolean[n + 1][n + 1];
int sum = 0;
for(int now : values)
sum += now;
return sum < 2*MemorySearch(0,values.length - 1, dp, flag, values);
}
int MemorySearch(int left, int right, int [][]dp, boolean [][]flag, int []values) {
if(flag[left][right])
return dp[left][right];
flag[left][right] = true;
if(left > right) {
dp[left][right] = 0;
} else if (left == right) {
dp[left][right] = values[left];
} else if(left + 1 == right) {
dp[left][right] = Math.max(values[left], values[right]);
} else {
int pick_left = Math.min(MemorySearch(left + 2, right, dp, flag, values), MemorySearch(left + 1, right - 1, dp, flag, values)) + values[left];
int pick_right = Math.min(MemorySearch(left, right - 2, dp, flag, values), MemorySearch(left + 1, right - 1, dp, flag, values)) + values[right];
dp[left][right] = Math.max(pick_left, pick_right);
}
return dp[left][right];
}