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".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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
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.
| 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. |
Minimum Difference Between Highest and Lowest of K Scores - Leetcode Weekly Contest - 1984 Python • NeetCode • 19,321 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