Problem
Given a list of 24-hour clock time points in “HH:MM” format, return the minimum minutes difference between any two time-points in the list.
Example 1:
Input: timePoints = ["23:59","00:00"] Output: 1
Example 2:
Input: timePoints = ["00:00","23:59","00:00"] Output: 0
Constraints:
2 <= timePoints.length <= 2 * 104
timePoints[i]
is in the format “HH:MM”.
Java Solution
class Solution {
public int findMinDifference(List<String> timePoints) {
int[] exist = new int[24 * 60];
for (String time : timePoints) {
int minutes = Integer.parseInt(time.substring(0, 2)) * 60 + Integer.parseInt(time.substring(3, 5));
if (exist[minutes] != 0) {
return 0;
} else {
exist[minutes] = 1;
}
}
int prev = -1;
int res = Integer.MAX_VALUE, start = Integer.MAX_VALUE, end = Integer.MIN_VALUE;
for (int i = 0; i < 24 * 60; i++) {
if (exist[i] == 1) {
if (prev == -1) {
prev = i;
} else {
res = Math.min(res, i - prev);
}
prev = i;
start = Math.min(start, i);
end = Math.max(end, i);
}
}
return Math.min(res, 24 * 60 - (end - start));
}
}