Convert Time Points to Minutes: Convert each time point from “HH:MM” format to the total number of minutes from 00:00.
Sort the Time Points: Sort the list of times in ascending order based on their total minutes representation.
Calculate Differences:
Calculate the difference between each consecutive time point in the sorted list.
Remember to consider the difference between the first and the last time point, accounting for the circular nature of the clock (i.e., the difference between “23:59” and “00:00”).
Find the Minimum Difference: Keep track of the minimum difference found during the comparisons.
classSolution:
deffindMinDifference(self, timePoints: List[str]) -> int:
defconvertToMinutes(time: str) -> int:
hours, minutes = time.split(":")
return int(hours) *60+ int(minutes)
# Convert all time points to minutes minutesList = [convertToMinutes(time) for time in timePoints]
# Sort the list of minutes minutesList.sort()
# Initialize the minimum difference to a large number minDifference = float("inf")
# Calculate differences between consecutive time pointsfor i in range(1, len(minutesList)):
diff = minutesList[i] - minutesList[i -1]
minDifference = min(minDifference, diff)
# Calculate the difference across the midnight boundary midnightDiff =1440+ minutesList[0] - minutesList[-1]
minDifference = min(minDifference, midnightDiff)
return minDifference