class Solution:
def findMinDifference(self, timePoints):
minutes = []
for time in timePoints:
h, m = map(int, time.split(":"))
minutes.append(h * 60 + m)
minutes.sort()
min_diff = 1440 # Maximum possible in 24h
for i in range(1, len(minutes)):
min_diff = min(min_diff, minutes[i] - minutes[i-1])
# Wrap around difference (last and first)
min_diff = min(min_diff, 1440 + minutes[0] - minutes[-1])
return min_diff
class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
vector<int> minutes;
for (const string& time : timePoints) {
int h = stoi(time.substr(0,2));
int m = stoi(time.substr(3,2));
minutes.push_back(h * 60 + m);
}
sort(minutes.begin(), minutes.end());
int minDiff = 1440;
for (int i = 1; i < minutes.size(); ++i) {
minDiff = min(minDiff, minutes[i] - minutes[i-1]);
}
minDiff = min(minDiff, 1440 + minutes[0] - minutes.back());
return minDiff;
}
};
class Solution {
public int findMinDifference(List<String> timePoints) {
List<Integer> minutes = new ArrayList<>();
for (String time : timePoints) {
String[] parts = time.split(":");
int h = Integer.parseInt(parts[0]);
int m = Integer.parseInt(parts[1]);
minutes.add(h * 60 + m);
}
Collections.sort(minutes);
int minDiff = 1440;
for (int i = 1; i < minutes.size(); ++i) {
minDiff = Math.min(minDiff, minutes.get(i) - minutes.get(i-1));
}
minDiff = Math.min(minDiff, 1440 + minutes.get(0) - minutes.get(minutes.size()-1));
return minDiff;
}
}
var findMinDifference = function(timePoints) {
let minutes = timePoints.map(time => {
let [h, m] = time.split(":").map(Number);
return h * 60 + m;
});
minutes.sort((a, b) => a - b);
let minDiff = 1440;
for (let i = 1; i < minutes.length; ++i) {
minDiff = Math.min(minDiff, minutes[i] - minutes[i-1]);
}
minDiff = Math.min(minDiff, 1440 + minutes[0] - minutes[minutes.length-1]);
return minDiff;
};
You are given a list of strings, timePoints
, where each string represents a time in the 24-hour format "HH:MM
". Your task is to find the minimum difference in minutes between any two time points in the list.
At first glance, it may seem that we need to compare every pair of time points to find the minimum difference. This brute-force approach would involve calculating the difference between all possible pairs, which can be slow if there are many time points.
However, since time wraps around at midnight, we need to be careful when calculating the difference. For example, the difference between "23:59" and "00:00" is only 1 minute, not 1439.
To optimize, we can convert all times to minutes since midnight, sort them, and then only need to check the differences between adjacent times in the sorted list (including the wrap-around case).
timePoints
to the number of minutes since midnight. For example, "01:30" becomes 90.1440 + first - last
).Sorting is used to efficiently find the smallest difference, as the closest times will be next to each other in the sorted order. Converting to minutes simplifies the calculations and makes wrap-around handling straightforward.
Let's consider the input ["23:59","00:00","12:30"]
.
This shows how the wrap-around case can produce the minimum difference.
The optimized approach is much faster for large inputs.
The key insight is to convert times to minutes, sort them, and only check adjacent pairs (including the wrap-around), rather than all possible pairs. This leverages sorting for efficiency and handles the day wrap-around elegantly, resulting in a concise and efficient solution.