Minimum Time Difference (LeetCode 539)
Time manipulation problems are common in coding interviews, and LeetCode Problem 539, “Minimum Time Difference,” is a great example. In this problem, we are given a list of time points in “HH:MM” format, and we need to find the minimum difference between any two time points.
Problem Statement:
You are given a list of time points in 24-hour format ("HH:MM"
). Your task is to find the minimum time difference between any two time points. The difference is calculated in minutes, and since time wraps around, you must account for the difference between the last and first time points as well.
For example:
Input: ["23:59","00:00"]
Output: 1
The difference between "23:59"
and "00:00"
is just 1 minute, which is the smallest possible difference.
Approach:
The most efficient way to solve this problem is by:
- Converting time to minutes: Convert all the time points to the number of minutes since midnight. For example,
"23:59"
becomes1439
minutes and"00:00"
becomes0
minutes. - Sorting the time points: Once the times are converted to minutes, sort them. The smallest difference will either be between consecutive time points or between the last and first time points (considering the wrap-around from
23:59
to00:00
). - Calculating differences: After sorting, calculate the difference between consecutive time points and wrap around the last and first point.
JavaScript Solution:
Here’s how you can implement this in JavaScript:
function findMinDifference(timePoints) {
// Helper function to convert time in "HH:MM" format to minutes
const toMinutes = (time) => {
const [hours, minutes] = time.split(":").map(Number);
return hours * 60 + minutes;
};
// Convert all time points to minutes and sort them
const minutesArray = timePoints.map(toMinutes).sort((a, b) => a - b);
let minDiff = Number.MAX_SAFE_INTEGER;
// Calculate the minimum difference between consecutive time points
for (let i = 1; i < minutesArray.length; i++) {
minDiff = Math.min(minDiff, minutesArray[i] - minutesArray[i - 1]);
}
// Calculate the circular difference (between the last and first time points)
const circularDiff = 1440 + minutesArray[0] - minutesArray[minutesArray.length - 1];
minDiff = Math.min(minDiff, circularDiff);
return minDiff;
}
// Example usage
const timePoints = ["23:59", "00:00", "12:34", "05:20"];
console.log(findMinDifference(timePoints)); // Output: 1
Explanation of the Code:
- Convert time to minutes: The
toMinutes
function splits the"HH:MM"
string and converts it into an integer representing the total minutes since midnight. This simplifies the comparison between times. - Sorting the time points: Once all times are converted to minutes, we sort the array. This ensures that consecutive differences can be easily calculated.
- Finding the minimum difference: After sorting, we iterate through the sorted array to calculate the differences between consecutive time points. Additionally, we calculate the circular difference between the last and the first time point to account for the wrap-around from
23:59
to00:00
. - Result: The smallest difference found is returned as the result.
Handling Edge Cases:
- Minimum time points: If there are fewer than two time points, this problem does not make sense. According to LeetCode’s problem constraints, the input always contains at least two time points, so we don’t need to handle that edge case explicitly.
- 24-hour wrap-around: Time “wraps around” from
23:59
to00:00
, which is why we calculate the circular difference between the last and first times.
Time Complexity:
The time complexity of this solution is O(n log n), where n
is the number of time points. This is because of the sorting step. The rest of the operations, such as converting the times and finding the differences, are linear (O(n)).
Conclusion:
This approach efficiently solves the problem by converting the time points to minutes, sorting them, and calculating the differences. It’s a great example of using sorting and modular arithmetic to handle circular data, such as times on a 24-hour clock.
By understanding how to manipulate and compare times, you’ll be better equipped to handle similar time-related problems in coding interviews. Practice this problem and try variations where different formats of time or constraints are given!
Feel free to leave any questions or suggestions in the comments below, and happy coding!