You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0200-0299/0292.Nim Game/README_EN.md
+14-1
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,20 @@ In all outcomes, your friend wins.
51
51
52
52
## Solutions
53
53
54
-
### Solution 1
54
+
### Solution 1: Finding the Pattern
55
+
56
+
The first player who gets a multiple of $4$ (i.e., $n$ can be divided by $4$) will lose the game.
57
+
58
+
Proof:
59
+
60
+
1. When $n \lt 4$, the first player can directly take all the stones, so the first player will win the game.
61
+
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.
62
+
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.
63
+
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.
64
+
1. ...
65
+
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.
66
+
67
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0200-0299/0293.Flip Game/README_EN.md
+46-28
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,13 @@
35
35
36
36
## Solutions
37
37
38
-
### Solution 1
38
+
### Solution 1: Traversal + Simulation
39
+
40
+
We traverse the string. If the current character and the next character are both `+`, we change these two characters to `-`, add the result to the result array, and then change these two characters back to `+`.
41
+
42
+
After the traversal ends, we return the result array.
43
+
44
+
The time complexity is $O(n^2)$, where $n$ is the length of the string. Ignoring the space complexity of the result array, the space complexity is $O(n)$ or $O(1)$.
0 commit comments