539. Minimum Time Difference¶
Description¶
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.
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 * 104
timePoints[i]
is in the format "HH:MM".
Solution: Sorting¶
Convert timePoints
to minutes
and sort them. The minimum difference must be the difference in an adjacent pair of times, if sort the minutes
in ascending order. Thus, we can retrieve the smallest difference by calculating the difference between each adjacent pair of elements from minutes
.
Why adjacent elements have smaller differences, compared to non-adjacent elements?
- Difference between adjacent elements \(a_i - a_{i+1}\)
- Difference between non-adjacent elements \(a_i - a_j, \text{where } j > i+1\)
- Due to Monotonic Sequence1 \(a_i \geq a_{i+1} \geq a_{i+2} \geq \ldots \geq a_n\), the difference between adjacent elements is always smaller than the difference between non-adjacent elements.
- Time Complexity:
O(N⋅logN)
,- Convert
timePoints
to minutes takesO(N)
time - Sorting
timePoints
using QuickSort which takesO(N⋅logN)
time
- Convert
- Space Complexity:
O(N)
minutes
array takesO(N)
space to store converted input
Solution: Bucket Sort¶
Since minutes
are falling in the range of 0
to 24*60
, we can use Bucket Sort which can solve the problem with linear time complexity.
- Time Complexity:
O(n)
O(n)
to converttimePoints
tominutes
O(1)
to find the minimum difference
- Space Complexity:
O(1)
minutes
always has a fixed size of24*60
elements
Why not considered it as Counting Sort?
- Some said that the solution is using Counting Sort instead of Bucket Sort which is not fully wrong. But Counting Sort considered as non-comparison-based sorting algorithm, and the solution did use comparison.
- Concept of Bucket Sort is dividing the range into multiple buckets, sorting each bucket, and then merging them.
minutes
looks like multiple buckets but only has one element inside each of them, even though the value in Bucket Sort normally will not be a binary(boolean).
What's the trade-off?
24*60
consideredO(1)
in theory, but it might impact the performance iftimePoints
is very large.- Additionally, the constant space array can be inefficient if many entries are not used which can cause overhead(higher memory resourse usage) and potentially slower performance than the sorting solution.