Mr. Panda's ball-passing game
$begingroup$
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. Panda 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. Panda 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. Panda 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();
}
}
java algorithm programming-challenge time-limit-exceeded
$endgroup$
add a comment |
$begingroup$
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. Panda 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. Panda 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. Panda 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();
}
}
java algorithm programming-challenge time-limit-exceeded
$endgroup$
add a comment |
$begingroup$
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. Panda 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. Panda 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. Panda 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();
}
}
java algorithm programming-challenge time-limit-exceeded
$endgroup$
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. Panda 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. Panda 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. Panda 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();
}
}
java algorithm programming-challenge time-limit-exceeded
java algorithm programming-challenge time-limit-exceeded
edited 3 mins ago
Jamal♦
30.3k11117227
30.3k11117227
asked 2 hours ago
Prashin JeevaganthPrashin Jeevaganth
1384
1384
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212782%2fmr-pandas-ball-passing-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212782%2fmr-pandas-ball-passing-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown