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
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
17
+
You are playing the following Nim Game with your friend:
18
+
There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones.
19
+
The one who removes the last stone will be the winner.
20
+
You will take the first turn to remove the stones.
10
21
11
-
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
22
+
Both of you are very clever and have optimal strategies for the game.
23
+
Write a function to determine whether you can win the game given the number of stones in the heap.
12
24
13
-
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
25
+
For example, if there are 4 stones in the heap,
26
+
then you will never win the game: no matter 1, 2, or 3 stones you remove,
27
+
the last stone will always be removed by your friend.
14
28
15
29
Hint:
16
30
17
-
If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
31
+
If there are 5 stones in the heap, could you figure out a way to remove the stones
32
+
such that you will always be the winner?
18
33
19
34
20
35
Hide Similar Problems (M) Flip Game II
21
36
22
37
*/
23
38
24
-
25
39
/*
26
40
Thoughts:
27
41
If n = 4, we can do the following:
@@ -45,4 +59,32 @@ public boolean canWinNim(int n) {
45
59
returnn % 4 != 0;
46
60
}
47
61
}
62
+
63
+
64
+
65
+
/*
66
+
Thoughts:
67
+
Game theory DP. Consider the last step:
68
+
for 1st player to win, the opponent needs to have the possibility to lose
69
+
(assume 1st player take the chance when seeing one)
70
+
71
+
Make dp[i] represents true/false 1st will win, if given i stones.
0 commit comments