Similar Problems

Similar Problems not available

New 21 Game - Leetcode Solution

Companies:

LeetCode:  New 21 Game Leetcode Solution

Difficulty: Medium

Topics: math sliding-window dynamic-programming  

Problem:

Alice plays the following game, where n is a positive integer: initially, she faces the number 0 on her calculator; in each move, she can perform one of the following operations:

  • Add any positive integer x to the number on the display.
  • Multiply the number on the display by 2.

Alice wins the game if, after a finite number of moves, the display shows a number that is greater than or equal to n. Alice loses otherwise.

Given an integer n, return true if Alice wins the game, assuming she plays first.

Example 1:

Input: n = 10 Output: true Explanation: Alice can perform the following moves:

  • Initially, the number is 0.
  • Alice adds 1 to get 1. Now the number is 1.
  • Alice multiplies the number by 2 to get 2. Now the number is 2.
  • Alice adds 1 to get 3. Now the number is 3.
  • Alice multiplies the number by 2 to get 6. Now the number is 6.
  • Alice multiplies the number by 2 to get 12. Now the number is 12.
  • Alice adds 1 to get 13. Alice wins because 13 >= 10.

Example 2:

Input: n = 6 Output: true Explanation: Alice can perform the following moves:

  • Initially, the number is 0.
  • Alice adds 1 to get 1. Now the number is 1.
  • Alice multiplies the number by 2 to get 2. Now the number is 2.
  • Alice multiplies the number by 2 to get 4. Now the number is 4.
  • Alice adds 1 to get 5. Now the number is 5.
  • Alice multiplies the number by 2 to get 10. Now the number is 10.
  • Alice adds 1 to get 11. Alice wins because 11 >= 6.

Example 3:

Input: n = 4 Output: false Explanation: Alice can perform the following moves:

  • Initially, the number is 0.
  • Alice adds 1 to get 1. Now the number is 1.
  • Alice multiplies the number by 2 to get 2. Now the number is 2.
  • Alice adds 1 to get 3. Now the number is 3.
  • Alice multiplies the number by 2 to get 6. Alice loses because 6 < 4.

Approach:

The approach for solving the New 21 Game problem is dynamic programming. We will consider the state of the game at i, which represents the value that Alice has on the calculator. We will also maintain a variable called S, which is the sum of the last W values, where W is the maximum single turn addition Alice can make. If S >= K, then Alice wins. At each state i, we will calculate the probability of reaching any state j that is i+1, i+2, i+3, …, i+W. The probability of reaching j from i is 1/W, assuming that Alice randomly chooses to add any number from 1 to W.

Algorithm:

  1. Initialize dp array with size of n+w+1 and assign zero to all of them.
  2. Fill probability 1 to dp[0], because if sum is zero the probability of winning is always 1.
  3. Declare a variable called S and initialize it with 1, which represents the probability of reaching from 0 to 1 with a single addition.
  4. Use variable sum to calculate the sum of the last w+1 probabilities in dp[] array.
  5. Iterate through numbers starting from 1 to n and fill the dp[] array with probabilities by using S and sum variables.
  6. At the end, check if the sum of probabilities from dp[] array for states greater than or equal to K is greater than or equal to 1, then Alice can win the game, otherwise return false.

Code:

class Solution { public boolean canWinNim(int n) { int w = 21; // choose W as 21 double[] dp = new double[n+w+1]; for (int i = 0; i < dp.length; i++) { dp[i] = 0.0; } dp[0] = 1.0; double S = 1.0; double sum = 1.0; for (int i = 1; i <= n; i++) { double probability = sum / w; dp[i] = probability; if (i < w) { sum += dp[i]; } else { sum += dp[i] - dp[i-w]; } } for (int i = n+1; i <= n+w; i++) { if (i >= n+w) { return dp[i] >= 1.0;
} } return false; } }

The time complexity of the solution is O(n).

New 21 Game Solution Code

1