Skip to content

Commit 080b52d

Browse files
solves #2682: Find the Losers of the Circular Game in java
1 parent cb3aa34 commit 080b52d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/find-the-losers-of-the-circular-game
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class FindTheLosersOfTheCircularGame {
9+
public int[] circularGameLosers(int n, int k) {
10+
final Set<Integer> players = new HashSet<>();
11+
for (int i = 0, turn = 1 ; ; i = (i + turn * k) % n, turn++) {
12+
if (players.contains(i)) {
13+
break;
14+
}
15+
players.add(i);
16+
}
17+
final int totalLosers = n - players.size();
18+
final int[] losers = new int[totalLosers];
19+
for (int i = 1, j = 0 ; i <= n ; i++) {
20+
if (!players.contains(i - 1)) {
21+
losers[j++] = i;
22+
}
23+
}
24+
return losers;
25+
}
26+
}

0 commit comments

Comments
 (0)