public int solution(int distance, int[][] scope, int[][] times) {
List<Integer> validTimes = findValidTimes(scope, times);
Collections.sort(validTimes);
if (!validTimes.isEmpty()) {
return validTimes.get(0);
}
return distance;
}
private List<Integer> findValidTimes(int[][] scope, int[][] times) {
List<Integer> validTimes = new ArrayList<>();
for (int i = 0; i < scope.length; i++) {
Arrays.sort(scope[i]);
int start = scope[i][0];
int end = scope[i][1];
int workTime = times[i][0];
int restTime = times[i][1];
while (start <= end) {
int timeCheck = start % (workTime + restTime);
if (timeCheck > 0 && timeCheck <= workTime) {
validTimes.add(start);
break;
}
start++;
}
}
return validTimes;
}