LeetCode 2682: Find the Losers of the Circular Game
Problem Description
Explanation
To solve this problem, we can simulate the game as described in the problem statement. We can keep track of the friends who receive the ball and mark the losers who do not receive the ball at any point during the game. We iterate through the friends in a circular manner, passing the ball to the next friend based on the given steps k
. Whenever a friend receives the ball for the second time, the game ends.
Time Complexity: O(n) Space Complexity: O(n)
Solutions
class Solution {
public List<Integer> findTheLosers(int n, int k) {
List<Integer> losers = new ArrayList<>();
boolean[] receivedBall = new boolean[n + 1];
int currentFriend = 1;
for (int i = 1; i <= n; i++) {
if (receivedBall[currentFriend]) {
losers.add(currentFriend);
} else {
receivedBall[currentFriend] = true;
}
currentFriend = (currentFriend + k) % n;
if (currentFriend == 0) {
currentFriend = n;
}
}
return losers;
}
}
Loading editor...