Created
September 8, 2021 08:42
-
-
Save codecakes/ecfd9129ca6aacd3a3b06ac3b54ed307 to your computer and use it in GitHub Desktop.
Find Minimum Time Difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
"""Given an array of time strings times, return the smallest difference between any two times in minutes. | |
Example: | |
Input: ["00:03", "23:59", "12:03"] | |
Output: 4 | |
Input: The closest 2 times are "00:03" and "23:59" (by wrap-around), and they differ by 4 minutes. | |
Constraints: | |
All strings will be non-empty and in the format HH:mm | |
""" | |
def timeDifference(self, times): | |
''' | |
:type times: list of str | |
:rtype: int | |
''' | |
total_mins = 24*60 | |
arr = [] | |
for idx, time in enumerate(times): | |
hh, mm = time.split(":") | |
hh, mm = int(hh), int(mm) | |
if hh == 0: | |
arr += [total_mins + (hh * 60) + mm] | |
else: | |
arr += [(hh * 60) + mm] | |
min_diff = float("INF") | |
sorted_arr = sorted(arr) | |
# print("sorted_arr", sorted_arr) | |
for idx, each_min in enumerate(sorted_arr): | |
if idx > 0: | |
min_diff = min(sorted_arr[idx] - sorted_arr[idx-1], min_diff) | |
return min_diff | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment