Skip to content

Commit fe0148d

Browse files
committed
leetcode
1 parent b0a40b2 commit fe0148d

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

NimGame/nim_game.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
3+
4+
-* Nim Game *-
5+
6+
7+
You are playing the following Nim Game with your friend:
8+
9+
Initially, there is a heap of stones on the table.
10+
You and your friend will alternate taking turns, and you go first.
11+
On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
12+
The one who removes the last stone is the winner.
13+
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.
14+
15+
16+
17+
Example 1:
18+
19+
Input: n = 4
20+
Output: false
21+
Explanation: These are the possible outcomes:
22+
1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins.
23+
2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins.
24+
3. You remove 3 stones. Your friend removes the last stone. Your friend wins.
25+
In all outcomes, your friend wins.
26+
Example 2:
27+
28+
Input: n = 1
29+
Output: true
30+
Example 3:
31+
32+
Input: n = 2
33+
Output: true
34+
35+
36+
Constraints:
37+
38+
1 <= n <= 231 - 1
39+
40+
41+
*/
42+
43+
class Solution {
44+
bool canWinNim(int n) {
45+
// if the remainder is not 0 because if there is remainder than nim will win
46+
return n % 4 != 0;
47+
}
48+
}
49+
50+
class B {
51+
bool canWinNim(int n) {
52+
// if the number is less than zero or equal nothing to do here
53+
if (n <= 0) return false;
54+
// because we can pick 0-3 pick one time so true
55+
if (n == 1 || n == 2 || n == 3) return true;
56+
// recursive to see after picking 0,1,2,3 if there is something left we lost
57+
if (canWinNim(n - 1) && canWinNim(n - 2) && canWinNim(n - 3)) return false;
58+
return true;
59+
}
60+
}
61+
62+
class C {
63+
bool canWinNim(int n) {
64+
if (n >= 134882061) return n % 4 != 0;
65+
bool result = true;
66+
bool first = true;
67+
bool second = true;
68+
bool third = true;
69+
for (int i = 4; i <= n; i++) {
70+
result = (first && second && third) ? false : true;
71+
first = second;
72+
second = third;
73+
third = result;
74+
}
75+
return result;
76+
}
77+
}

NimGame/nim_game.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

NimGame/nim_game.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 🔥 Nim Game 🔥 || || 3 Approaches Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
bool canWinNim(int n) {
8+
// if the remainder is not 0 because if there is remainder than nim will win
9+
return n % 4 != 0;
10+
}
11+
}
12+
```
13+
14+
## Solution - 2 Recursive
15+
16+
```dart
17+
class Solution {
18+
bool canWinNim(int n) {
19+
// if the number is less than zero or equal nothing to do here
20+
if (n <= 0) return false;
21+
// because we can pick 0-3 pick one time so true
22+
if (n == 1 || n == 2 || n == 3) return true;
23+
// recursive to see after picking 0,1,2,3 if there is something left we lost
24+
if (canWinNim(n - 1) && canWinNim(n - 2) && canWinNim(n - 3)) return false;
25+
return true;
26+
}
27+
}
28+
```
29+
30+
## Solution - 3
31+
32+
```dart
33+
class Solution {
34+
bool canWinNim(int n) {
35+
if (n >= 134882061) return n % 4 != 0;
36+
bool result = true;
37+
bool first = true;
38+
bool second = true;
39+
bool third = true;
40+
for (int i = 4; i <= n; i++) {
41+
result = (first && second && third) ? false : true;
42+
first = second;
43+
second = third;
44+
third = result;
45+
}
46+
return result;
47+
}
48+
}
49+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
119119
- [Move Zeroes](MoveZeroes/move_zeroes.dart)
120120
- [Where Will the Ball Fall](WhereWillTheBallFall/where_will_the_ball_fall.dart)
121121
- [Word Pattern](WordPattern/word_pattern.dart)
122+
- [Nim Game](NimGame/nim_game.dart)
122123

123124
## Reach me via
124125

0 commit comments

Comments
 (0)