Intuition¶
The problem involves finding the minimum time difference between multiple time points in a 24-hour format. The key insight is that time can be converted to minutes from midnight, making it easier to calculate differences. Once converted, sorting the time points and calculating the smallest difference between adjacent times allows us to solve the problem efficiently. We also need to handle the case where the last and first time points are close but wrap around midnight.
Approach¶
- Convert each time point into minutes from midnight. This simplifies comparison.
- Sort the array of times in minutes to make it easier to calculate the differences.
- Iterate through the sorted array to find the minimum difference between adjacent time points.
- Consider the wraparound case where the difference between the last and first time points spans across midnight.
- If the number of time points exceeds 1440 (the total number of minutes in a day), return 0 early, since there must be a duplicate.
Complexity¶
-
Time complexity:
The time complexity is $O(n \log n)$ due to the sorting step, where $$n$$ is the number of time points. The subsequent operations (parsing and iterating through the sorted list) are linear, i.e., $O(n)$. -
Space complexity:
The space complexity is $O(n)$, since we store the time points in minutes in an array of sizen.
Code¶
class Solution {
public int findMinDifference(List<String> timePoints) {
int N = timePoints.size();
if (N > 1440) {
return 0;
}
int[] timeInMinutes = new int[N];
for (int i = 0; i < N; i++) {
String time = timePoints.get(i);
int hours = (time.charAt(0) - '0') * 10 + (time.charAt(1) - '0');
int minutes = (time.charAt(3) - '0') * 10 + (time.charAt(4) - '0');
timeInMinutes[i] = hours * 60 + minutes;
}
Arrays.sort(timeInMinutes);
int currentMin = Integer.MAX_VALUE;
for (int i = 1; i < N; i++) {
currentMin = Math.min(currentMin, timeInMinutes[i] - timeInMinutes[i - 1]);
}
currentMin = Math.min(currentMin, 1440 + timeInMinutes[0] - timeInMinutes[N - 1]);
return currentMin;
}
}