Minimum Time Difference — Solution in Python, Java & C++
Minimum Time Difference solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Minimum Time Difference is a medium coding problem on arrays, strings and math, asked in Amazon, Google, Microsoft and Capital One interviews.
- Difficulty: Medium
- Topics: Arrays, Strings, Math, Sorting
- Asked at: Amazon, Google, Microsoft, Capital One, Palantir
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given a list of clock times on a 24-hour clock, each written "HH:MM" (00:00 to 23:59). The clock wraps around: 23:59 and 00:00 are one minute apart.
Return the smallest difference, in minutes, between any two of the times. Equal times are 0 minutes apart.
Example 1
Input: timePoints = ["09:30","23:55","00:10"]
Output: 15
Explanation: From 23:55 to 00:10 is 15 minutes across midnight.
Example 2
Input: timePoints = ["12:00","18:45","12:00"]
Output: 0
Example 3
Input: timePoints = ["00:00","12:00"]
Output: 720
Constraints
2 <= timePoints.length <= 2 * 10^4timePoints[i] is in the format "HH:MM"
Hints for Minimum Time Difference
Hint 1
Convert every time to minutes after midnight.
Hint 2
After sorting, the closest pair is always adjacent — except that the last and first times are also neighbours across midnight.
Hint 3
There are only 1440 distinct minutes: with more times than that a duplicate is guaranteed, and a 1440-slot bucket replaces the sort.
How to solve Minimum Time Difference
On a circle of 1440 minutes the closest pair of points are neighbours in sorted order, with the last point also neighbouring the first through midnight.
Approach
- Mark each time's minute
60·HH + MMin an array of 1440 slots; if a slot is already marked, return 0. - Walk the slots in order, remembering the first and the previous marked minute; for each marked minute, update the answer with its distance to the previous one.
- Finally update the answer with the wrap-around gap
first + 1440 − last.
Why it works
For two non-adjacent sorted minutes some marked minute lies between them on the shorter arc, which makes one of the adjacent gaps no larger, so checking adjacent gaps (including the wrap-around one) suffices. The circular distance between neighbours x < y is min(y − x, 1440 − (y − x)), and the adjacent gaps around the circle sum to 1440, so whichever direction is shorter appears as one of those gaps.
Complexity
- Time —
O(n + 1440) - Space —
O(1440)
Pitfalls
- Forgetting the gap across midnight (
23:55→00:10). - Duplicate times give 0 — the bucket approach detects them immediately.
- Comparing the strings directly instead of converting to minutes.
Reference solution
Python
from typing import List
def findMinDifference(timePoints: List[str]) -> int:
seen = [False] * 1440
for t in timePoints:
m = int(t[:2]) * 60 + int(t[3:])
if seen[m]:
return 0
seen[m] = True
first = prev = -1
best = 1440
for m in range(1440):
if seen[m]:
if prev >= 0:
best = min(best, m - prev)
else:
first = m
prev = m
return min(best, first + 1440 - prev)Java
public static int findMinDifference(String[] timePoints) {
boolean[] seen = new boolean[1440];
for (String t : timePoints) {
int m = Integer.parseInt(t.substring(0, 2)) * 60 + Integer.parseInt(t.substring(3));
if (seen[m]) return 0;
seen[m] = true;
}
int first = -1, prev = -1, best = 1440;
for (int m = 0; m < 1440; m++) {
if (!seen[m]) continue;
if (prev >= 0) best = Math.min(best, m - prev);
else first = m;
prev = m;
}
return Math.min(best, first + 1440 - prev);
}C++
int findMinDifference(vector<string>& timePoints) {
vector<bool> seen(1440, false);
for (const string& t : timePoints) {
int m = stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3));
if (seen[m]) return 0;
seen[m] = true;
}
int first = -1, prev = -1, best = 1440;
for (int m = 0; m < 1440; m++) {
if (!seen[m]) continue;
if (prev >= 0) best = min(best, m - prev);
else first = m;
prev = m;
}
return min(best, first + 1440 - prev);
}JavaScript
var findMinDifference = function(timePoints) {
var seen = new Array(1440);
for (var i = 0; i < timePoints.length; i++) {
var t = timePoints[i];
var m = parseInt(t.substring(0, 2), 10) * 60 + parseInt(t.substring(3), 10);
if (seen[m]) return 0;
seen[m] = true;
}
var first = -1, prev = -1, best = 1440;
for (var m2 = 0; m2 < 1440; m2++) {
if (!seen[m2]) continue;
if (prev >= 0) best = Math.min(best, m2 - prev);
else first = m2;
prev = m2;
}
return Math.min(best, first + 1440 - prev);
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.