07. Difference Check
β GFG solution to the Minimum Time Difference problem: find minimum difference in seconds between any two time strings using efficient sorting technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[]
of time strings in 24-hour clock format "HH:MM:SS". Your task is to find the minimum difference in seconds between any two time strings in the array.
The clock wraps around at midnight, so the time difference between "23:59:59" and "00:00:00" is 1 second.
π Examples
Example 1
Input: arr[] = ["12:30:15", "12:30:45"]
Output: 30
Explanation: The minimum time difference is 30 seconds.
Example 2
Input: arr[] = ["00:00:01", "23:59:59", "00:00:05"]
Output: 2
Explanation: The time difference is minimum between "00:00:01" and "23:59:59".
π Constraints
$2 \le \text{arr.size()} \le 10^5$
$\text{arr}[i]$ is in "HH:MM:SS" format.
β
My Approach
The optimal approach uses Time Conversion and Sorting to efficiently find the minimum difference:
Time Parsing + Sorting Algorithm
Convert Time to Seconds:
Parse each time string "HH:MM:SS" and convert to total seconds from midnight.
Formula:
seconds = hours * 3600 + minutes * 60 + seconds
This normalizes all times to a single integer format for easy comparison.
Sort the Seconds:
Sort all converted time values in ascending order.
This allows us to check adjacent pairs for minimum differences efficiently.
Find Minimum Adjacent Differences:
Iterate through sorted array and calculate difference between consecutive elements.
Keep track of the minimum difference found.
Handle Circular Clock:
Consider the wrap-around case: difference between the last time and first time of next day.
Calculate:
first_time + 86400 - last_time
(86400 = total seconds in a day).Compare this with other differences to find the global minimum.
Early Termination:
If any two times are identical, return 0 immediately.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the array. The time conversion takes O(n) time, but sorting dominates with O(n log n) complexity.
Expected Auxiliary Space Complexity: O(n), where n is the size of the array. We use additional space to store the converted seconds in a vector/array.
π§βπ» Code (C++)
class Solution {
public:
int minDifference(vector<string>& arr) {
vector<int> mins;
for (auto& t : arr) {
int h = (t[0] - '0') * 10 + (t[1] - '0');
int m = (t[3] - '0') * 10 + (t[4] - '0');
int s = (t[6] - '0') * 10 + (t[7] - '0');
mins.push_back(h * 3600 + m * 60 + s);
}
sort(mins.begin(), mins.end());
int res = mins[0] + 86400 - mins.back();
for (int i = 1; i < mins.size(); i++) {
res = min(res, mins[i] - mins[i-1]);
}
return res;
}
};
β Code (Java)
class Solution {
public int minDifference(String[] arr) {
int[] mins = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
String t = arr[i];
int h = (t.charAt(0) - '0') * 10 + (t.charAt(1) - '0');
int m = (t.charAt(3) - '0') * 10 + (t.charAt(4) - '0');
int s = (t.charAt(6) - '0') * 10 + (t.charAt(7) - '0');
mins[i] = h * 3600 + m * 60 + s;
}
Arrays.sort(mins);
int res = mins[0] + 86400 - mins[mins.length - 1];
for (int i = 1; i < mins.length; i++) {
res = Math.min(res, mins[i] - mins[i - 1]);
}
return res;
}
}
π Code (Python)
class Solution:
def minDifference(self, arr):
mins = []
for t in arr:
h = int(t[:2])
m = int(t[3:5])
s = int(t[6:8])
mins.append(h * 3600 + m * 60 + s)
mins.sort()
res = mins[0] + 86400 - mins[-1]
for i in range(1, len(mins)):
res = min(res, mins[i] - mins[i-1])
return res
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated