Leetcode #539: Minimum Time Difference
In this guide, we solve Leetcode #539 Minimum Time Difference in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
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".
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Array, Math, String, Sorting
Intuition
Sorting reveals structure that is hard to see in the original order.
Once sorted, a linear scan is usually enough to compute the answer.
Approach
Sort the data and sweep through it while maintaining a small state.
This keeps the logic straightforward and reliable.
Steps:
- Sort the data.
- Scan in order while maintaining state.
- Update the best answer.
Example
Input: timePoints = ["23:59","00:00"]
Output: 1
Python Solution
class Solution:
def findMinDifference(self, timePoints: List[str]) -> int:
if len(timePoints) > 1440:
return 0
nums = sorted(int(x[:2]) * 60 + int(x[3:]) for x in timePoints)
nums.append(nums[0] + 1440)
return min(b - a for a, b in pairwise(nums))
Complexity
The time complexity is , and the space complexity is , where is the number of time points. The space complexity is , where is the number of time points.
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.