2024-09-16 Daily Challenge
Today I have done leetcode's September LeetCoding Challenge with cpp
.
September LeetCoding Challenge 16
Description
Minimum Time Difference
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
class Solution {
int diff(const string &a, const string &b) {
return (a[0] - b[0]) * 600 + (a[1] - b[1]) * 60 + (a[3] - b[3]) * 10 + a[4] - b[4];
}
public:
int findMinDifference(vector<string>& timePoints) {
sort(timePoints.begin(), timePoints.end());
int answer = diff(timePoints.front(), timePoints.back()) + 1440;
for(int i = 1; i < timePoints.size(); ++i) {
answer = min(answer, diff(timePoints[i], timePoints[i - 1]));
}
return answer;
}
};