Leetcode 539: Minimum Time Difference
Given a list of time points in ‘HH:MM’ format, return the minimum time difference between any two distinct time points in the list.
Problem
Approach
Steps
Complexity
Input: The input is a list of time points in 'HH:MM' format.
Example: Input: timePoints = ['23:58', '00:05']
Constraints:
• 2 <= timePoints.length <= 2 * 10^4
• timePoints[i] is in 'HH:MM' format
Output: Return the minimum time difference between any two time points in minutes.
Example: Output: 7
Constraints:
• The returned value is the smallest time difference between any two distinct time points in the list.
Goal: Find the minimum time difference between any two distinct time points in the list.
Steps:
• Sort the list of time points in ascending order.
• Calculate the differences between consecutive time points.
• Also calculate the difference between the first and last time points, as the time range is circular.
• Return the minimum of all differences.
Goal: The time points are guaranteed to be in valid 'HH:MM' format, and the list contains at least two time points.
Steps:
• The input list contains between 2 and 2 * 10^4 time points.
• Each time point is a valid time in 'HH:MM' format.
Assumptions:
• The input list contains at least two time points.
• Input: Input: timePoints = ['23:58', '00:05']
• Explanation: The minimum time difference between 23:58 and 00:05 is 7 minutes.
Approach: To solve this problem, we need to find the minimum difference in minutes between two time points in the list. Sorting the time points makes it easier to calculate the differences between consecutive points and handle the circular nature of the 24-hour clock.
Observations:
• Sorting the list of time points allows us to compare consecutive points to find the minimum difference.
• We must also consider the circular nature of the clock, where the earliest time and the latest time might have the smallest difference.
Steps:
• Convert each time point to minutes since midnight.
• Sort the time points in ascending order.
• Calculate the difference between consecutive time points, as well as the difference between the first and last time point (taking into account the circular nature of the clock).
• Return the minimum of these differences.
Empty Inputs:
• The input list will always contain at least two time points, so there are no empty inputs.
Large Inputs:
• Ensure that the solution efficiently handles lists with up to 2 * 10^4 time points.
Special Values:
• Handle time points close to midnight (e.g., '23:59' and '00:00').
Constraints:
• The input list contains valid time points in 'HH:MM' format.
int findMinDifference(vector<string>& time) {
sort(time.begin(), time.end());
int n = time.size(), mindiff = INT_MAX;
for(int i = 0; i < n; i++) {
int diff = abs(timeDiff(time[(i - 1 +n)%n], time[i]));
diff = min(diff, 1440 - diff); // 1440 = 24h in minutes
mindiff = min(mindiff, diff);
}
return mindiff;
}
int timeDiff(string t1, string t2) {
// cout << t1 << t2;
int h1 = stoi(t1.substr(0, 2));
int m1 = stoi(t1.substr(3, 2));
int h2 = stoi(t2.substr(0, 2));
int m2 = stoi(t2.substr(3, 2));
return (h2 - h1) * 60 + (m2 - m1);
}
1 : Function Definition
int findMinDifference(vector<string>& time) {
Defines the `findMinDifference` function which takes a vector of time strings as input and returns the minimum difference between any two times in minutes.
2 : Sort Operation
sort(time.begin(), time.end());
Sorts the time strings in lexicographical order, ensuring that time differences are calculated in a sequential manner.
3 : Variable Initialization
int n = time.size(), mindiff = INT_MAX;
Initializes variables: `n` holds the size of the `time` vector, and `mindiff` is set to the maximum integer value to track the minimum difference.
4 : Loop
for(int i = 0; i < n; i++) {
Begins a loop that iterates over all time strings, comparing adjacent times to calculate their differences.
5 : Time Difference Calculation
int diff = abs(timeDiff(time[(i - 1 +n)%n], time[i]));
Calculates the absolute time difference between the current time and the previous time (using modulo for circular indexing).
6 : Min Time Difference Adjustment
diff = min(diff, 1440 - diff); // 1440 = 24h in minutes
Adjusts the time difference to account for the wrap-around of the day, ensuring that the minimum difference is correctly calculated, even across midnight.
7 : Min Difference Update
mindiff = min(mindiff, diff);
Updates the `mindiff` variable to store the smallest time difference encountered during the loop.
8 : Return Statement
return mindiff;
Returns the minimum time difference found during the loop.
9 : Helper Function Definition
int timeDiff(string t1, string t2) {
Defines the helper function `timeDiff`, which calculates the time difference in minutes between two time strings.
10 : Time Parsing
int h1 = stoi(t1.substr(0, 2));
Extracts the hour component from the first time string `t1` and converts it to an integer.
11 : Time Parsing
int m1 = stoi(t1.substr(3, 2));
Extracts the minute component from the first time string `t1` and converts it to an integer.
12 : Time Parsing
int h2 = stoi(t2.substr(0, 2));
Extracts the hour component from the second time string `t2` and converts it to an integer.
13 : Time Parsing
int m2 = stoi(t2.substr(3, 2));
Extracts the minute component from the second time string `t2` and converts it to an integer.
14 : Time Difference Calculation
return (h2 - h1) * 60 + (m2 - m1);
Calculates and returns the time difference in minutes between the two time strings `t1` and `t2`.
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Description: The time complexity is O(n log n) due to the sorting step, where n is the number of time points.
Best Case: O(n)
Worst Case: O(n)
Description: The space complexity is O(n) due to the storage required for the sorted list of time points.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus