-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNim Game.cs
37 lines (23 loc) · 966 Bytes
/
Nim Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Solution {
public bool CanWinNim(int n) {
if (n%4 !=0)
return true;
else
return false;
}
}
/*This is a straightforward code that uses the concept of winning-losing positions. A position is losing if from that position, if you took any number of allowed stones [1,2,3] you would go only to winning positions and a position is considered winning if there's at least one move that can lead you to a losing position.
Initially all the available moves [1,2,3] are winning positions, then we can easily derive the following:
1 W
2 W
3 W
4 L (1,2,3 lead to W)
5 W (1 leads to L)
6 W (2 leads to L)
7 W (3 leads to L)
8 L
9 W
10 W
..
So we can see that only multiple of 4s are losing positions for the first player, while all other numbers are winning.*/
//Explanation regards to https://discuss.leetcode.com/topic/30682/1-liner-java-solution-with-nim-game-explanation/2