|
1 |
| ---- |
2 |
| -comments: true |
3 |
| -difficulty: Easy |
4 |
| -edit_url: https://github.com/doocs/leetcode/edit/main/solution/0200-0299/0292.Nim%20Game/README_EN.md |
5 |
| -tags: |
6 |
| - - Brainteaser |
7 |
| - - Math |
8 |
| - - Game Theory |
9 |
| ---- |
10 |
| - |
11 |
| -<!-- problem:start --> |
12 |
| - |
13 |
| -# [292. Nim Game](https://leetcode.com/problems/nim-game) |
14 |
| - |
15 |
| -[中文文档](/solution/0200-0299/0292.Nim%20Game/README.md) |
16 |
| - |
17 |
| -## Description |
18 |
| - |
19 |
| -<!-- description:start --> |
20 |
| - |
21 |
| -<p>You are playing the following Nim Game with your friend:</p> |
22 |
| - |
23 |
| -<ul> |
24 |
| - <li>Initially, there is a heap of stones on the table.</li> |
25 |
| - <li>You and your friend will alternate taking turns, and <strong>you go first</strong>.</li> |
26 |
| - <li>On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.</li> |
27 |
| - <li>The one who removes the last stone is the winner.</li> |
28 |
| -</ul> |
29 |
| - |
30 |
| -<p>Given <code>n</code>, the number of stones in the heap, return <code>true</code><em> if you can win the game assuming both you and your friend play optimally, otherwise return </em><code>false</code>.</p> |
31 |
| - |
32 |
| -<p> </p> |
33 |
| -<p><strong class="example">Example 1:</strong></p> |
34 |
| - |
35 |
| -<pre> |
36 |
| -<strong>Input:</strong> n = 4 |
37 |
| -<strong>Output:</strong> false |
38 |
| -<strong>Explanation:</strong> These are the possible outcomes: |
39 |
| -1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins. |
40 |
| -2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins. |
41 |
| -3. You remove 3 stones. Your friend removes the last stone. Your friend wins. |
42 |
| -In all outcomes, your friend wins. |
43 |
| -</pre> |
44 |
| - |
45 |
| -<p><strong class="example">Example 2:</strong></p> |
46 |
| - |
47 |
| -<pre> |
48 |
| -<strong>Input:</strong> n = 1 |
49 |
| -<strong>Output:</strong> true |
50 |
| -</pre> |
51 |
| - |
52 |
| -<p><strong class="example">Example 3:</strong></p> |
53 |
| - |
54 |
| -<pre> |
55 |
| -<strong>Input:</strong> n = 2 |
56 |
| -<strong>Output:</strong> true |
57 |
| -</pre> |
58 |
| - |
59 |
| -<p> </p> |
60 |
| -<p><strong>Constraints:</strong></p> |
61 |
| - |
62 |
| -<ul> |
63 |
| - <li><code>1 <= n <= 2<sup>31</sup> - 1</code></li> |
64 |
| -</ul> |
65 |
| - |
66 |
| -<!-- description:end --> |
67 |
| - |
68 |
| -## Solutions |
69 |
| - |
70 |
| -<!-- solution:start --> |
71 |
| - |
72 |
| -### Solution 1: Finding the Pattern |
73 |
| - |
74 |
| -The first player who gets a multiple of $4$ (i.e., $n$ can be divided by $4$) will lose the game. |
75 |
| - |
76 |
| -Proof: |
77 |
| - |
78 |
| -1. When $n \lt 4$, the first player can directly take all the stones, so the first player will win the game. |
79 |
| -1. When $n = 4$, no matter whether the first player chooses $1, 2, 3$, the second player can always choose the remaining number, so the first player will lose the game. |
80 |
| -1. When $4 \lt n \lt 8$, i.e., $n = 5, 6, 7$, the first player can correspondingly reduce the number to $4$, then the "death number" $4$ is given to the second player, and the second player will lose the game. |
81 |
| -1. When $n = 8$, no matter whether the first player chooses $1, 2, 3$, it will leave a number between $4 \lt n \lt 8$ to the second player, so the first player will lose the game. |
82 |
| -1. ... |
83 |
| -1. By induction, when a player gets the number $n$, and $n$ can be divided by $4$, he will lose the game, otherwise, he will win the game. |
84 |
| - |
85 |
| -The time complexity is $O(1)$, and the space complexity is $O(1)$. |
86 |
| - |
87 |
| -<!-- tabs:start --> |
88 |
| - |
89 |
| -#### Python3 |
90 |
| - |
91 |
| -```python |
92 |
| -class Solution: |
93 |
| - def canWinNim(self, n: int) -> bool: |
94 |
| - return n % 4 != 0 |
95 |
| -``` |
96 |
| - |
97 |
| -#### Java |
98 |
| - |
99 |
| -```java |
100 |
| -class Solution { |
101 |
| - public boolean canWinNim(int n) { |
102 |
| - return n % 4 != 0; |
103 |
| - } |
104 |
| -} |
105 |
| -``` |
106 |
| - |
107 |
| -#### C++ |
108 |
| - |
109 |
| -```cpp |
110 |
| -class Solution { |
111 |
| -public: |
112 |
| - bool canWinNim(int n) { |
113 |
| - return n % 4 != 0; |
114 |
| - } |
115 |
| -}; |
116 |
| -``` |
117 |
| -
|
118 |
| -#### Go |
119 |
| -
|
120 |
| -```go |
121 |
| -func canWinNim(n int) bool { |
122 |
| - return n%4 != 0 |
123 |
| -} |
124 |
| -``` |
125 |
| - |
126 |
| -#### TypeScript |
127 |
| - |
128 |
| -```ts |
129 |
| -function canWinNim(n: number): boolean { |
130 |
| - return n % 4 != 0; |
131 |
| -} |
132 |
| -``` |
133 |
| - |
134 |
| -#### Rust |
135 |
| - |
136 |
| -```rust |
137 |
| -impl Solution { |
138 |
| - pub fn can_win_nim(n: i32) -> bool { |
139 |
| - n % 4 != 0 |
140 |
| - } |
141 |
| -} |
142 |
| -``` |
143 |
| - |
144 |
| -<!-- tabs:end --> |
145 |
| - |
146 |
| -<!-- solution:end --> |
147 |
| - |
148 |
| -<!-- problem:end --> |
| 1 | +Esy question |
| 2 | +NIM GAME |
| 3 | +if(n%4!=0) return true; |
| 4 | +return false, |
0 commit comments