Minimum Time Difference (LeetCode 539): Solution and Explanation
You get a list of "HH:MM" times on a 24-hour clock. Return the smallest number of minutes between any two of them, remembering that the clock wraps from 23:59 back to 00:00.
Intuition
Picture the times as dots on a clock face. The two closest dots must sit next to each other on the circle: if some dot lay between them, it would be closer to one of them. So you never need to compare every pair, only each dot with its clockwise neighbour.
Converting each time to minutes since midnight (hours * 60 + minutes) turns the clock into the numbers 0 to 1439. Walking them in increasing order gives you the neighbours, and the one gap that is easy to forget is the arc from the last time, past midnight, to the first time: first + 1440 - last.
Because there are only 1440 possible minutes, you can skip sorting. Light up a slot per minute in a 1440-entry array, and a slot that is already lit is a duplicate, which means the answer is 0. More than 1440 times guarantees such a duplicate before you even look.
Approach: Bucket the minutes and sweep the dial
- If there are more than 1440 times, return 0, since two of them must share a minute.
- For each time, compute
m = hours * 60 + minutes. Ifseen[m]is already true, return 0; otherwise set it. - Sweep
mfrom 0 to 1439. At the first lit slot, recordfirst. At every later lit slot, updatebest = min(best, m - prev). Then setprev = m. - Return
min(best, first + 1440 - prev)to include the gap across midnight.
Step-by-step example
timePoints = ["23:59", "00:00"]
- There are 2 times, far fewer than 1440, so no forced duplicate.
- "23:59" becomes minute 1439 and "00:00" becomes minute 0. Neither slot was lit before.
- The sweep reaches minute 0 first, so
first = 0andprev = 0. - The sweep reaches minute 1439: the gap is 1439 - 0 = 1439, so
best = 1439, andprev = 1439. - The midnight gap is 0 + 1440 - 1439 = 1, which is smaller than 1439. Return 1.
Code: Python, Java, C++
1class Solution:
2 def findMinDifference(self, timePoints: List[str]) -> int:
3 if len(timePoints) > 1440:
4 return 0
5 seen = [False] * 1440
6 for t in timePoints:
7 m = int(t[:2]) * 60 + int(t[3:])
8 if seen[m]:
9 return 0
10 seen[m] = True
11 first = prev = -1
12 best = 1440
13 for m in range(1440):
14 if not seen[m]:
15 continue
16 if prev == -1:
17 first = m
18 else:
19 best = min(best, m - prev)
20 prev = m
21 return min(best, first + 1440 - prev)1class Solution {
2 public int findMinDifference(List<String> timePoints) {
3 if (timePoints.size() > 1440) return 0;
4 boolean[] seen = new boolean[1440];
5 for (String t : timePoints) {
6 int m = Integer.parseInt(t.substring(0, 2)) * 60 + Integer.parseInt(t.substring(3));
7 if (seen[m]) return 0;
8 seen[m] = true;
9 }
10 int first = -1, prev = -1, best = 1440;
11 for (int m = 0; m < 1440; m++) {
12 if (!seen[m]) continue;
13 if (prev == -1) first = m;
14 else best = Math.min(best, m - prev);
15 prev = m;
16 }
17 return Math.min(best, first + 1440 - prev);
18 }
19}1class Solution {
2public:
3 int findMinDifference(vector<string>& timePoints) {
4 if (timePoints.size() > 1440) return 0;
5 vector<bool> seen(1440, false);
6 for (const string& t : timePoints) {
7 int m = stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3));
8 if (seen[m]) return 0;
9 seen[m] = true;
10 }
11 int first = -1, prev = -1, best = 1440;
12 for (int m = 0; m < 1440; m++) {
13 if (!seen[m]) continue;
14 if (prev == -1) first = m;
15 else best = min(best, m - prev);
16 prev = m;
17 }
18 return min(best, first + 1440 - prev);
19 }
20};Time and space complexity
O(n)
Each time is parsed once, and the sweep over 1440 slots is a constant amount of extra work.
O(1)
The boolean array always has 1440 entries regardless of the input size.
Other approaches
Sort the minutes (time O(n log n), space O(n))
Convert every time to minutes, sort the list, compare adjacent entries and finally the wrap gap from the last to the first. It is the most common answer and needs no pigeonhole trick, but sorting costs O(n log n).
Compare every pair (time O(n^2), space O(1))
For each pair compute d = |a - b| and take min(d, 1440 - d). It is correct but quadratic, which is too slow for 20,000 times.
Common mistakes
- Forgetting the gap across midnight. With 23:59 and 00:00 the plain difference is 1439, but the real answer is 1.
- Comparing the time strings directly instead of converting to minutes, which makes the subtraction meaningless.
- Returning early only for exact duplicate strings but parsing badly, for example reading "05:07" as 5 * 60 + 70 by slicing the wrong characters.
- Using 24 * 60 = 1440 slots but checking
first + 1439 - prevfor the wrap, which is off by one.
Frequently asked questions
Why is it enough to check neighbouring times?
If two times are not neighbours on the circle, some third time lies between them along the shorter arc, and that third time is strictly closer to one of them. So the minimum is always achieved by a pair of neighbours.
Why return 0 when there are more than 1440 times?
A day has 1440 distinct minutes. With more times than minutes, the pigeonhole principle says two times must be equal, and equal times differ by 0.
Is the 1440-slot array really better than sorting?
Asymptotically yes: it runs in O(n + 1440) instead of O(n log n). For the given limits both are fast, so either is a fine interview answer as long as the midnight gap is handled.
Does the wrap gap need to be checked if best is already 1?
It cannot make the answer worse, so taking the minimum is always safe. You can return early when best reaches 0 or 1 as an optimization, but it is not required.