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 * 104timePoints[i] is in the format "HH:MM".Problem Overview: You receive a list of time points in HH:MM format. The task is to compute the smallest difference in minutes between any two time points. Because time wraps around midnight (e.g., 23:59 and 00:00), the comparison must also consider the circular nature of a 24‑hour clock.
Approach 1: Sorting and Finding Minimum Difference (O(n log n) time, O(n) space)
Convert each time string into total minutes from midnight (hour * 60 + minute). Store these values in an array and sort them. Once sorted, iterate through adjacent elements and compute the difference between each pair to track the minimum gap. The circular edge case is handled by comparing the last time of the day with the first time plus 1440 minutes. Sorting simplifies the comparison logic because the closest times must appear next to each other in the ordered list.
This method relies on basic operations from array processing and sorting. It works well for general input sizes and is easy to implement in most languages. Time complexity comes from sorting (O(n log n)), while the scan for differences runs in O(n). Space complexity is O(n) due to the converted minute array.
Approach 2: Using a Boolean Array to Track Minutes (O(n) time, O(1) space)
A day contains exactly 1440 minutes. Instead of sorting, allocate a boolean array of size 1440 and mark each minute that appears in the input. Convert every time string to its minute index and check if that index is already marked. If it is, the minimum difference is immediately 0 because duplicate times exist.
After marking all time points, scan the boolean array to locate consecutive marked minutes and compute their differences. Track the first and last seen minutes to correctly compute the circular difference across midnight. Because the array size is fixed (1440), the scan runs in constant time relative to the input size.
This technique uses ideas from array indexing and simple math transformations. The algorithm runs in O(n) time for inserting time points and O(1440) for scanning, which simplifies to O(n). Space complexity is effectively O(1) since the 1440‑length array is constant regardless of input size.
Recommended for interviews: The sorting approach is the most commonly expected solution because it is straightforward and demonstrates clean reasoning about ordering and edge cases like the midnight wrap‑around. Mentioning the boolean‑array optimization shows deeper insight: you recognize the bounded domain of minutes in a day and reduce the complexity to linear time. Showing both approaches signals strong problem‑solving depth in interviews.
Convert the time into minutes from "00:00", sort, then find the smallest difference between any two adjacent times while also considering the difference across midnight.
This implementation works by converting each time point to the number of minutes since 00:00. We sort these times and then calculate the adjacent differences. We also calculate the wraparound difference from the last to the first 24-hour period to ensure capturing the smallest difference across midnight.
Time Complexity: O(n log n) due to sorting where n is the number of time points. Space Complexity: O(n) since we store the times in minutes.
Mark each minute of the day in a boolean array once any time point corresponds to it. Then traverse the array to find the smallest gap between marked minutes, considering wrap-around. This is efficient because it eliminates the need for sorting.
Here, a boolean array representing each minute of the day is used to track time points. The algorithm finds the smallest gap between the marked minutes, ensuring wrap-around difference is checked at the end.
Time Complexity: O(n + M), where M is 1440, the number of minutes in a day, n is the length of list.
Space Complexity: O(M), M = 1440, fixed.
We notice that there can be at most 24 times 60 = 1440 distinct time points. Therefore, if the length of timePoints exceeds 1440, it implies there are duplicate time points, and we can return 0 early.
Next, we iterate through the list of time points and convert it into a list of minutes nums. For example, for the time point 13:14, we convert it into 13 times 60 + 14.
Then, we sort the list of minutes in ascending order and append the smallest time nums[0] plus 1440 to the end of the list. This step is to handle the special case of the difference between the maximum and minimum values.
Finally, we iterate through the list of minutes to find the minimum difference between any two adjacent times.
The time complexity is O(n log n), and the space complexity is O(n), where n is the number of time points.
| Approach | Complexity |
|---|---|
| Sorting and Finding Minimum Difference | Time Complexity: O(n log n) due to sorting where n is the number of time points. Space Complexity: O(n) since we store the times in minutes. |
| Using a Boolean Array to Track Minutes | Time Complexity: O(n + M), where M is 1440, the number of minutes in a day, n is the length of list. |
| Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting and Finding Minimum Difference | O(n log n) | O(n) | General solution that is simple to implement and commonly expected in interviews |
| Boolean Array for 1440 Minutes | O(n) | O(1) | When exploiting the fixed 24‑hour domain for a faster linear-time solution |
LeetCode 539. Minimum Time Difference (Solution Explained) • Nick White • 13,635 views views
Watch 9 more video solutions →Practice Minimum Time Difference with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor