Last active
May 28, 2021 09:52
-
-
Save Julisam/fe4e68b213c748da752e4945ff65a06b to your computer and use it in GitHub Desktop.
AlgorithmFriday_Week7
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
def shortest_interval(time_arr = []): | |
if time_arr == None or not isinstance(time_arr, list): | |
return 0 | |
else: | |
# convert time_array to minutes | |
timeInMin = [] | |
for time in time_arr: | |
try: | |
h,m = time.split(':') | |
if int(h)<24 and int(m)<60: | |
timeInMin.append(int(h)*60 + int(m)) | |
except: | |
continue; | |
# in case all entries in the array does does not follow the time format | |
if len(timeInMin)==0: return 0 | |
# if we have only one element in the timeInMin list | |
if len(timeInMin)==1: return timeInMin[0] | |
# if we have only two time intervals, we just minus | |
if len(timeInMin)==2: return abs(timeInMin[0] - timeInMin[1]) | |
# Sort array in non-decreasing order | |
timeInMin = sorted(timeInMin) | |
# Initialize difference as infinite | |
min_diff = 10**20 | |
# Find the min diff by comparing adjacent | |
# pairs in sorted array | |
for i in range(len(timeInMin)-1): | |
if timeInMin[i+1] - timeInMin[i] < min_diff: | |
min_diff = timeInMin[i+1] - timeInMin[i] | |
# Return min diff | |
return min_diff | |
print(shortest_interval(['16:15', '16:00', '12:20'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Julisam, congratulations 🎉 your solution has been selected as one of the winning solutions in Week 7 of #AlgorithmFridays.
Out of the many winning solutions, only 3 will be selected for the 20$ prize in a raffle draw. The raffle draw will hold today, Friday May 28 at 3.00pm WAT (7.00 am PST)
If you are interested in being a part of the raffle draw, please send me a DM on Twitter @meekg33k so I can share the event invite with you.
NB: Only solutions of participants who indicated interest in the raffle draw will be considered.
Thanks once again for participating and see you later today for Week 8 of #AlgorithmFridays.