Skip to content

Commit 7306c29

Browse files
authored
292. Nim Game
1 parent cf639a7 commit 7306c29

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Brute Force/canWinNim.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
```
2+
You are playing the following Nim Game with your friend:
3+
4+
Initially, there is a heap of stones on the table.
5+
You and your friend will alternate taking turns, and you go first.
6+
On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
7+
The one who removes the last stone is the winner.
8+
9+
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.
10+
11+
12+
13+
Example 1:
14+
15+
Input: n = 4
16+
Output: false
17+
Explanation: These are the possible outcomes:
18+
1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins.
19+
2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins.
20+
3. You remove 3 stones. Your friend removes the last stone. Your friend wins.
21+
In all outcomes, your friend wins.
22+
23+
Example 2:
24+
25+
Input: n = 1
26+
Output: true
27+
28+
Example 3:
29+
30+
Input: n = 2
31+
Output: true
32+
33+
```
34+
35+
class Solution {
36+
public:
37+
bool canWinNim(int n) {
38+
return n%4 != 0;
39+
}
40+
41+
};

0 commit comments

Comments
 (0)