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
if (n <= 0) return false;
boolean[] dp = new boolean[2];
dp[0] = false;
dp[1] = true;
for (int i = 2; i <= n; i++) {
dp[i % 2] = !(dp[(i - 1) % 2] && dp[(i - 2) % 2]);
}
return dp[n % 2];
}
}
No comments:
Post a Comment