I have an assignment to solve this problem. There are a total of 18 test case files, 5 of which I have failed due to exceeding some time limit on the script and I am unable to verify if there are any logic errors thereafter.
Question Description
Mr. Muffin is an orientation group leader and sets up a simple ice breaking game. N students initially sit in a circle with one of them holding a ball. Every time Mr. Muffin shouts “NEXT”, the student holding the ball passes it to the next student clockwise. The student that receives the ball will have to shout their name.
Occasionally, the student holding the ball might want to “LEAVE” the circle (going to the toilet for example). In that case, the student will pass the ball to the next student clockwise in the circle and leave. The student receiving the ball will shout their name as well.
New students might also arrive and want to “JOIN” the circle. In this case, the new student will sit at the position such that they will be the next student to receive the ball. The ball will then be immediately passed to the new student and the new student will have shout their name.
As the number of students increases, Mr. Muffin finds it very difficult to keep track of all the students and their names. Thus, given the list of events that happen (either NEXT, LEAVE or JOIN), he wants you to code a program to tell him what name will be shouted at each time.
Even though the students are sitting in a circle which is non-linear, Rar the Cat suggests that clever usage of Java API Linear Data Structures would be sufficient to solve this task. (i.e. You do not need to implement your own data structure for this task.)
Input
The first line contains a single integer N, the number of students initially in the circle.
The second line contains N strings, representing the names of the students in the circle in a clockwise manner. The first name in the list is the name of the student currently holding the ball.
The third line contains a single integer Q, the number of events that happen.
The next Q lines represent the events that happen in chronological order. The first string of each line will either be NEXT, LEAVE or JOIN, representing the event. If the event is JOIN, then that line will contain a second string representing the name of the student that joined.
Output
The output should contain Q lines, representing the names that are shouted in the order of the events.
Limits
- 3≤N,Q≤200,000
It is guaranteed that every student’s name will consist of only English letters and be at most 10 characters long. However, the names of students might not be distinct. For instance, there can be more than 1 student with the name ‘Bob’.
It is also guaranteed that there will be at least 3 people in the circle at any point in time.
Sample input
3 Alice Bob Charlie 4 NEXT JOIN Donald NEXT LEAVE
Sample output
Bob Donald Charlie Alice
Does anyone have a solution to my problem? Do inform me if any more information is needed.
import java.util.*;
public class Ballpassing {
private void run() {
//implement your "main" method here
LinkedList<String> lst = new LinkedList<>();
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
for (int i = 0; i < length; i++) {
String name = sc.next();
lst.add(name);
}
int idx = 0;
int count = sc.nextInt();
for (int i = 0; i < count; i++) {
int nextIdx = (idx + 1)%lst.size();
String cmd = sc.next();
//handles the case where going to the head of the list is required, by moving the list rightwards
if (nextIdx == 0) {
LinkedList<String> shifted = (LinkedList<String>) lst.clone();
String element = shifted.getLast();
shifted.remove(idx);
shifted.addFirst(element);
idx = 0;
nextIdx = 1;
lst = shifted;
}
if (cmd.equals("LEAVE")) {
System.out.println(lst.get(nextIdx));
lst.remove(idx);
continue;
} else if (cmd.equals("JOIN")) {
String name = sc.next();
lst.add(nextIdx,name);
}
idx = nextIdx;
System.out.println(lst.get(idx));
}
}
public static void main(String[] args) {
Ballpassing newBallpassing = new Ballpassing();
newBallpassing.run();
}
}