Minimum Time Difference - Problem

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.

The time difference should be calculated as the shortest circular distance on a 24-hour clock. For example, the difference between "23:59" and "00:01" is 2 minutes (not 1438 minutes).

Note: The answer is guaranteed to be in the range [0, 720] since the maximum possible difference between any two times in a day is 12 hours (720 minutes).

Input & Output

Example 1 — Basic Case
$ Input: timePoints = ["23:59","00:01","00:03"]
Output: 2
💡 Note: The minimum difference is between "23:59" and "00:01" which is 2 minutes (going from 23:59 to 00:01 crosses midnight)
Example 2 — Same Hour
$ Input: timePoints = ["00:00","23:59","00:00"]
Output: 0
💡 Note: There are duplicate times "00:00", so the minimum difference is 0 minutes
Example 3 — Mid-day Times
$ Input: timePoints = ["12:30","14:15","13:45"]
Output: 30
💡 Note: Sorted: 12:30, 13:45, 14:15. Minimum is between 13:45 and 14:15 which is 30 minutes

Constraints

  • 2 ≤ timePoints.length ≤ 2 × 104
  • timePoints[i] is in the format "HH:MM"

Visualization

Tap to expand
Minimum Time Difference INPUT 0/24 6 12 18 1 00:01 00:03 23:59 Time Points Array: "23:59" "00:01" "00:03" 24-hour circular clock 1440 minutes total ALGORITHM STEPS 1 Convert to Minutes 23:59 = 1439 min 00:01 = 1 min, 00:03 = 3 min 2 Sort Minutes Array [1439, 1, 3] --> [1, 3, 1439] 1 3 1439 3 Compare Adjacent 3-1=2, 1439-3=1436 Min so far: 2 4 Check Circular Wrap 1440 - 1439 + 1 = 2 (first - last + 1440) 23:59 00:01 2 min gap FINAL RESULT Minimum gap found between 23:59 and 00:01 Output: 2 minutes OK - Verified Time: O(n log n) Space: O(n) Key Insight: The 24-hour clock is circular! After sorting, check all adjacent pairs PLUS the wrap-around case (first element + 1440 - last element). This handles times like 23:59 and 00:01 being only 2 minutes apart, not 1438 minutes. Convert HH:MM to minutes: hours * 60 + minutes. TutorialsPoint - Minimum Time Difference | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
890 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