Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created March 22, 2022 13:43
Show Gist options
  • Select an option

  • Save les-peters/8a5c16ddd9da7e5b090eca506c47ff3c to your computer and use it in GitHub Desktop.

Select an option

Save les-peters/8a5c16ddd9da7e5b090eca506c47ff3c to your computer and use it in GitHub Desktop.
Smallest Time Interval
question = """
Given a list of times in a 24-hour period, return the smallest interval between two
times in the list. Hint: you can do this in O(n) time!
Example:
$ smallestTimeInterval(['01:00', '08:15', '11:30', '13:45', '14:10', '20:05'])
$ '25 minutes'
"""
def smallestTimeInterval(times):
hrmns = sorted(list(map(lambda x: 60 * int(x[0]) + int(x[1]), (map(lambda x: x.split(":"), times)))))
diff = 86400
for i in range(0, len(hrmns)-1):
if (hrmns[i+1] - hrmns[i]) < diff:
diff = hrmns[i+1] - hrmns[i]
return str(diff) + " minutes"
print(smallestTimeInterval(['01:00', '08:15', '11:30', '13:45', '14:10', '20:05']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment