HomeLeetcode292. Nim Game - Leetcode Solutions

292. Nim Game – Leetcode Solutions

Description

You are playing the following Nim Game with your friend:

  • Initially, there is a heap of stones on the table.
  • You and your friend will alternate taking turns, and you go first.
  • On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
  • The one who removes the last stone is the winner.

Given n, the number of stones in the heap, return true if you can win the game assuming both you and your friend play optimally, otherwise return false.

Examples:

Example 1:

Input: n = 4
Output: false
Explanation: These are the possible outcomes:
1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins.
2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins.
3. You remove 3 stones. Your friend removes the last stone. Your friend wins.
In all outcomes, your friend wins.

Example 2:

Input: n = 1
Output: true

Example 3:

Input: n = 2
Output: true

Solution in Python

Key Insight:

  • If the number of stones (n) is a multiple of 4, you will always lose if your opponent plays optimally. This is because, whatever number of stones you take (1, 2, or 3), the opponent can always take enough stones to leave you with another multiple of 4 on your next turn.
  • On the other hand, if n is not a multiple of 4, you can win by reducing the number of stones in such a way that your opponent will face a multiple of 4.

Plan:

  • If n % 4 == 0, you cannot win. No matter what you do, your friend can always adjust the number of stones to leave you with a multiple of 4.
  • If n % 4 != 0, you can win by making sure your opponent gets a multiple of 4 stones after your turn.
Python
class Solution:
    def canWinNim(self, n: int) -> bool:
        # If n is a multiple of 4, you will always lose if both play optimally
        # Otherwise, you can win
        return n % 4 != 0

Explanation:

  1. The function takes an integer n, which is the number of stones in the heap.
  2. The condition n % 4 != 0 checks whether n is not a multiple of 4. If it’s not, you can guarantee a win by playing optimally.
  3. If n is a multiple of 4, the function returns False, indicating that you cannot win.

Solution in C++

C++
class Solution {
public:
    bool canWinNim(int n) {
        // If n is a multiple of 4, you will lose if both play optimally.
        // Otherwise, you can win.
        return n % 4 != 0;
    }
};

Solution in Javascript

JavaScript
var canWinNim = function(n) {
    // If n is divisible by 4, you cannot win if both players play optimally
    // Otherwise, you can win
    return n % 4 !== 0;
};

Solution in Java

Java
class Solution {
    public boolean canWinNim(int n) {
        // If n is a multiple of 4, you will lose if both players play optimally.
        // Otherwise, you can win.
        return n % 4 != 0;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular