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
Time as Points on a Clock Face00:00 (Midnight)06:0012:00 (Noon)18:0023:5900:0103:302 minKey Insight: After sorting times, minimum difference is between adjacent times or wrap-around!
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.2K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen