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
思路: 丢到pq里面,算i , i + 1 的差值,最后需要看一下last, first的差值;
class Solution {
public int findMinDifference(List<String> timePoints) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(String time: timePoints) {
int hour = Integer.parseInt(time.substring(0, 2));
int mins = Integer.parseInt(time.substring(3));
int totaltime = hour * 60 + mins;
pq.offer(totaltime);
}
if(pq.size() < 2) {
return 0;
}
int diff = Integer.MAX_VALUE;
int first = pq.poll();
int cur = first;
while(!pq.isEmpty()) {
int next = pq.poll();
diff = Math.min(diff, next - cur);
cur = next;
}
//算最后一个和第一个diff;
return Math.min(diff, 24 * 60 - cur + first);
}
}