Skip to content

Instantly share code, notes, and snippets.

@balamark
Last active November 4, 2017 17:24
Show Gist options
  • Save balamark/56665755c627af00f4f64f1bee10ee4c to your computer and use it in GitHub Desktop.
Save balamark/56665755c627af00f4f64f1bee10ee4c to your computer and use it in GitHub Desktop.
public int periodLength(int[] heaps) {
ArrayList<Integer> slow = new ArrayList<Integer>();
for (int i=0; i<heaps.length; i++) slow.add(heaps[i]);
Collections.sort(slow);
ArrayList<Integer> fast = slow;
while (true) {
slow = step(slow);
fast = step(fast);
fast = step(fast);
if (slow.equals(fast)) {
int period = 0;
while (true) {
slow = step(slow);
period++;
if (slow.equals(fast)) return period;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment