Minimum Time Difference - Problem
Imagine you're building a scheduling system that needs to find the closest time slots between appointments. Given a list of time points in 24-hour format ("HH:MM"), you need to find the minimum difference in minutes between any two time points.
This problem becomes interesting when you consider that time is circular - the difference between 23:59 and 00:01 is only 2 minutes, not 23 hours and 58 minutes!
Goal: Return the smallest possible time difference in minutes between any pair of time points.
Example: For times ["23:59", "00:00", "12:30"], the minimum difference is 1 minute (between 23:59 and 00:00).
Input & Output
example_1.py โ Basic Case
$
Input:
["23:59", "00:00"]
โบ
Output:
1
๐ก Note:
The difference between 23:59 and 00:00 is 1 minute (crossing midnight). This demonstrates the wrap-around behavior of time.
example_2.py โ Multiple Times
$
Input:
["00:00", "23:59", "00:00"]
โบ
Output:
0
๐ก Note:
There are duplicate times (00:00 appears twice), so the minimum difference is 0 minutes.
example_3.py โ Regular Times
$
Input:
["12:30", "14:15", "16:45"]
โบ
Output:
105
๐ก Note:
The differences are: 12:30โ14:15 = 105 min, 14:15โ16:45 = 150 min, 16:45โ12:30 (wrap) = 1065 min. Minimum is 105.
Constraints
- 2 โค timePoints.length โค 2 ร 104
- timePoints[i] is in the format "HH:MM"
- All times are valid 24-hour format times
Visualization
Tap to expand
Understanding the Visualization
1
Place Times on Clock
Convert each time to minutes from midnight and visualize as points on a circular clock
2
Sort Chronologically
Arrange times in chronological order around the clock face
3
Check Adjacent Arcs
Measure the distance between each consecutive pair of times
4
Check Wrap-around
Don't forget the arc from the last time back to the first time (crossing midnight)
Key Takeaway
๐ฏ Key Insight: Time is circular! After sorting, we only need to check adjacent differences plus the wrap-around from last to first time, reducing from O(nยฒ) to O(n) comparisons after sorting.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code