To solve Minimum Time Difference, convert all time strings to minutes and sort the resulting array. Compare consecutive differences and include the circular case from last to first to ensure the absolute minimum. This approach leverages array sorting and modular arithmetic to guarantee correctness while maintaining optimal performance for large inputs.
Problem Statement
Given a list of time points in 24-hour "HH:MM" format, find the minimum difference in minutes between any two times. The difference is measured on a circular clock, meaning the day wraps from 23:59 to 00:00.
Return the smallest difference in minutes. Each time point is guaranteed to be a valid 24-hour format, and duplicates may appear, making zero the minimum in some cases.
Examples
Example 1
Input: timePoints = ["23:59","00:00"]
Output: 1
Example details omitted.
Example 2
Input: timePoints = ["00:00","23:59","00:00"]
Output: 0
Example details omitted.
Constraints
- 2 <= timePoints.length <= 2 * 104
- timePoints[i] is in the format "HH:MM".
Solution Approach
Convert Times to Minutes
Iterate through the array of time strings, split each into hours and minutes, and convert each to total minutes. This allows straightforward numerical comparison instead of string manipulation.
Sort and Compute Differences
Sort the array of minute values, then compute differences between consecutive elements. Track the minimum difference found in this linear pass, which identifies the smallest gap efficiently.
Handle Circular Difference
Include the difference between the last and first element across midnight using modulo 1440 to account for the day wrap-around. This ensures the circular nature of time is respected and no minimal gap is missed.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(N) |
| Space | O(1) |
Time complexity is O(N) for converting and sorting if bucket counting is used, otherwise O(N log N) with standard sort. Space complexity is O(1) aside from the array of minutes since in-place computations are possible.
What Interviewers Usually Probe
- Asks about handling duplicates and zero-minute differences.
- Checks awareness of circular time calculation and modulo logic.
- Tests for edge cases like adjacent midnight values.
Common Pitfalls or Variants
Common pitfalls
- Forgetting the wrap-around from 23:59 to 00:00 and missing minimal differences.
- Incorrectly comparing string times instead of numeric minutes.
- Not handling duplicate times which can produce zero difference.
Follow-up variants
- Compute maximum time difference instead of minimum.
- Allow times with seconds and compute minimal difference including seconds.
- Find k smallest differences among all time points.
How GhostInterview Helps
- Automatically converts time strings to numeric values for immediate difference computation.
- Detects edge cases with duplicates and circular wrap-around to prevent incorrect answers.
- Provides step-by-step comparison between sorted times to quickly identify minimal gaps.
Topic Pages
Related GhostInterview Pages
- LeetCode Interview Copilot - Use GhostInterview as a live solver when you want direct help with LeetCode-style coding questions.
- Coding Interview Assistant - See how GhostInterview supports array, string, linked list, graph, and tree interview workflows.
- How GhostInterview Works - Review the screenshot, reasoning, and answer flow before using the solver in a live interview.
FAQ
What is the main approach for Minimum Time Difference?
Convert all time strings to minutes, sort the array, then check consecutive differences including the circular midnight case.
Can timePoints contain duplicates?
Yes, duplicates are allowed and can result in a minimum difference of zero.
Why do we need to consider circular differences?
Because the clock wraps from 23:59 to 00:00, ignoring this can miss the true minimal difference.
What is the optimal time complexity?
Using bucket sort or counting sort, the problem can be solved in O(N) time, otherwise standard sorting gives O(N log N).
Which pattern does this problem follow?
This is an Array plus Math pattern where array manipulation and numeric calculations are combined for efficient solution.
Need direct help with Minimum Time Difference instead of spending more time grinding it?
Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Time Difference from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.
Capture the prompt fast instead of rewriting the problem by hand.
Get the solution path, trade-offs, and complexity summary in one pass.
Stay outside captured layers on supported screen-share workflows.
Stay in the same pattern family
Find the longest word in the dictionary that can be formed by deleting characters from a string, using two-pointer scanning.
Open problem page#522 Longest Uncommon Subsequence IIFind the longest string in an array that is not a subsequence of any other string, using array scanning and hash checks efficiently.
Open problem page#472 Concatenated WordsFind concatenated words by using dynamic programming and depth-first search to identify valid words made of other words in the list.
Open problem page