Skip to content

Instantly share code, notes, and snippets.

@amilcar-andrade
Last active October 15, 2016 19:09
Show Gist options
  • Save amilcar-andrade/733575410b662d770d6a8c10ba2821d7 to your computer and use it in GitHub Desktop.
Save amilcar-andrade/733575410b662d770d6a8c10ba2821d7 to your computer and use it in GitHub Desktop.
SantaSecret basic implementation
import java.util.*;
public class SantaSecret {
public static void main(String[] args) {
String[][] list = generateList(new String[]{"amilcar", "andrade", "garcia"});
for (int i = 0; i < list.length; i++) {
System.out.println("From: " + list[i][0] + " To: " + list[i][1]);
}
}
private static String[][] generateList(String[] names) {
if (names == null) {
throw new NullPointerException("names array cannot be null");
}
if (names.length == 1) {
throw new RuntimeException("More than one item is needed!");
}
int length = names.length;
// From : To
String[][] result = new String[length][2];
Queue<String> q = new ArrayDeque<>(length);
for (int i = 0; i < length; i++) {
q.add(names[i]);
}
for (int i = 0; i < names.length; i++) {
if (names[i].equals(q.peek())) {
// Same person, move it to the end of the queue
String tempName = q.remove();
q.add(tempName);
}
// result [ row ] [ column ]
result[i][0] = names[i];
result[i][1] = q.remove();
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment