aboutsummaryrefslogtreecommitdiff
path: root/min_time_difference.py
diff options
context:
space:
mode:
Diffstat (limited to 'min_time_difference.py')
-rw-r--r--min_time_difference.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/min_time_difference.py b/min_time_difference.py
new file mode 100644
index 0000000..829b491
--- /dev/null
+++ b/min_time_difference.py
@@ -0,0 +1,30 @@
+# 539. 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
+"""
+
+
+class Solution(object):
+ def findMinDifference(self, timePoints):
+ """
+ :type timePoints: List[str]
+ :rtype: int
+ """
+
+ def convert(time):
+ return int(time[:2]) * 60 + int(time[3:])
+
+ minutes = map(convert, timePoints)
+ minutes.sort()
+
+ return min(
+ (y - x) % (24 * 60) for x, y in zip(minutes, minutes[1:] + minutes[:1])
+ )
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.findMinDifference(timePoints=["23:59", "00:00"]))
+ print(obj.findMinDifference(timePoints=["00:00", "23:59", "00:00"]))