コード
import java.util.ArrayDeque;
public class LongestSequence {
private int[] C;
private final int MAX_C = 1000;
public int maxLength(int[] C) {
this.C = C;
if (!isLoop(MAX_C * 2)) {
return -1;
}
int low = 0, high = MAX_C * 2;
while (high - low > 1) {
int mid = (low + high) / 2;
if (isLoop(mid)) {
high = mid;
} else {
low = mid;
}
}
return low;
}
private boolean isLoop(int ceil) {
boolean[] visit = new boolean[ceil + 1];
ArrayDeque<Integer> deque = new ArrayDeque<>();
visit[0] = true;
deque.push(0);
while (!deque.isEmpty()) {
int poll = deque.poll();
for (int c : C) {
int next = poll + c;
if (next == 0) {
return true;
}
if (next > 0 && next <= ceil && !visit[next]) {
visit[next] = true;
deque.push(next);
}
}
}
return false;
}
}