Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. 

Example 1:

Input: ["23:59","00:00"]
Output: 1



Note:

The number of time points in the given list is at least 2 and won't exceed 20000.
The input time is legal and ranges from 00:00 to 23:59.

Difficulty: interview

Starter code:

class Solution:
    def findMinDifference(self, timePoints: List[str]) -> int:
        

Solutions:
class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         if len(timePoints) > 1440: return 0
         timeNum = [0] * len(timePoints)
         for i in range(len(timePoints)):
             timeNum[i] = 60 * int(timePoints[i][:2]) + int(timePoints[i][3:])
             
         timeNum.sort()
         minMin = 24 * 60
         for i in range(len(timeNum) - 1):
             minMin = min(minMin, timeNum[i + 1] - timeNum[i])
         minMin = min(minMin, 24 * 60 + timeNum[0] - timeNum[-1])
         return minMin

class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         for i in range(len(timePoints)):
             t = timePoints[i]
             h = ''
             m = ''
             deb = True
             for j in range(len(t)):
                 if t[j]==':':
                     deb = False
                 elif deb:
                     h+=t[j]
                 else:
                     m+=t[j]
             timePoints[i] = int(m) + 60*int(h)
         
         timePoints.sort()
         print(timePoints)
         mi = float("inf")
         for i in range(len(timePoints)-1):
             diff = timePoints[i+1]-timePoints[i]
             if diff<mi:
                 mi = diff
         last = (24*60-timePoints[-1])+timePoints[0]
         if last<mi:
             mi = last
         return mi


#
 # [539] Minimum Time Difference
 #
 # https://leetcode.com/problems/minimum-time-difference/description/
 #
 # algorithms
 # Medium (46.11%)
 # Total Accepted:    15.7K
 # Total Submissions: 34.1K
 # Testcase Example:  '["23:59","00:00"]'
 #
 # Given a list of 24-hour clock time points in "Hour:Minutes" format, find the
 # minimum minutes difference between any two time points in the list.
 #
 # Example 1:
 #
 # Input: ["23:59","00:00"]
 # Output: 1
 #
 #
 #
 # Note:
 #
 # The number of time points in the given list is at least 2 and won't exceed
 # 20000.
 # The input time is legal and ranges from 00:00 to 23:59.
 #
 #
 #
 class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         exist = [False] * 1440
         low, high = 1440, 0
         for p in timePoints :
             t = int(p[: 2]) * 60 + int(p[3 :])
             if exist[t] :
                 return 0
             exist[t] = True
             low, high = min(low, t), max(high, t)
         diff = low + 1440 - high
         left, right = low, low + 1
         while left < high :
             while right <= high and not exist[right] :
                 right += 1
             diff = min(diff, right - left)
             left, right = right, right + 1
         return diff


class Solution:
     def findMinDifference(self, timePoints):
         timePoints = sorted(map(lambda x: [int(n) for n in x.split(':')], timePoints))
         timePoints.append([timePoints[0][0] + 24, timePoints[0][1]])
         res = float('inf')
         for i in range(1, len(timePoints)):
             diff = (timePoints[i][0] - timePoints[i - 1][0]) * 60 + (timePoints[i][1] - timePoints[i - 1][1])
             if diff > 720:
                 diff = 1440 - diff
             res = min(res, diff)
         return res

class Solution:
     def timetominute(self, time1):
         t1 = int(time1[:2]) * 60 + int(time1[3:5])
         return t1
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         count = 1440
         for i in range(len(timePoints)):
             timePoints[i] = self.timetominute(timePoints[i])
         timePoints.sort(reverse = False)
         print(timePoints)
         for i in range(len(timePoints)):
             if (timePoints[(i + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + 1 + len(timePoints)) % len(timePoints) -1]) < 0:
                 count = min(min((timePoints[(i + 1 + len(timePoints)) % len(timePoints) -1] - timePoints[(i + len(timePoints)) % len(timePoints) - 1]), (timePoints[(i + len(timePoints)) % len(timePoints) - 1] - (timePoints[(i + 1 + len(timePoints)) % len(timePoints) -1 ] ) + 1440)),count)
             else:
                 count = min(min((timePoints[(i + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + 1 + len(timePoints)) % len(timePoints) -1]), (timePoints[(i + 1 + len(timePoints)) % len(timePoints) -1] - timePoints[(i + len(timePoints)) % len(timePoints) - 1] + 1440)),count)
         return count


class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         uniques = set()
         for i in range(len(timePoints)):
             timePoints[i] = int(timePoints[i][:2]) * 60 + int(timePoints[i][-2:])
             if timePoints[i] in uniques:
                 return 0
             uniques.add(timePoints[i])
         uniques.clear()
         
         timePoints.sort()
         minimum = timePoints[0] + 24*60 - timePoints[-1]
         for i in range(1,len(timePoints)):
             temp = timePoints[i] - timePoints[i-1]
             if temp < minimum:
                 minimum = temp
         return minimum

class Solution:
     def convertToInt(self, time):
         hours = int(time.split(':')[0])
         mins = int(time.split(':')[1])
         return 60*hours+mins
 
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         s = set()
         for time in timePoints:
             mins = self.convertToInt(time)
             if mins in s:
                 return 0
             else:
                 s.add(mins)
         
         result = 12*60
         s = sorted(s)
         N = len(s)
         prev = s[N-1] - 24*60
         for i in range(N):
             if s[i] - prev < result:
                 result = s[i] - prev
             prev = s[i]
         return result
             
             


class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         s=[0]*1440
         for c in timePoints:
             t=(int(c[0])*10+int(c[1]))*60+int(c[3])*10+int(c[4])
             if s[t]:
                 return 0
             s[t]=1
         a=[]
         for i in range(1440):
             if s[i]==1:
                 a.append(i)
         m=a[0]+1440-a[-1]
         for i in range(1,len(a)):
             m=min(m,a[i]-a[i-1])
         return m

class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         def cv2mnt(t):
             h,m = t.split(':')
             mnt = int(h) * 60 + int(m)
             return mnt
         lst = [0 for i in range(1440)]
         for t in timePoints:
             mnt = cv2mnt(t)
             if lst[mnt] == 1:
                 return 0
             else:
                 lst[mnt] = 1
         beg = 0
         stk = [i for i,n in enumerate(lst) if n == 1]
         stk.append(stk[0] + 1440)
         res = 1439
         for i in range(len(stk) - 1):
             dif = abs(stk[i] - stk[i+1])
             dif_ = min(1440 - dif, dif)
             res = min(res,dif_)
             if res == 1:
                 return 1
         return res


def minDifference(minsA, minsB):
     maxMins = 60 * 24
     earlier = min(minsA, minsB)
     later = max(minsA, minsB)
     direct = later - earlier
     loopAround = maxMins - later + earlier
     return min(direct, loopAround)
 
 def toMinutes(timeString):
     h = int(timeString.split(":")[0])
     m = int(timeString.split(":")[1])
     return m + h * 60
 
 class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         # timePoints.sort()
         # currentMin = 60 * 24 + 1
         # for i in range(-1, len(timePoints) - 1):
         #     currentMin = min(currentMin, minDifference(toMinutes(timePoints[i]), toMinutes(timePoints[i+1])))
         # return currentMin
         minBuckets = [None] * (24 * 60)
         for i in range(24 * 60):
             minBuckets[i] = False
             
         for time in timePoints:
             if minBuckets[toMinutes(time) - 1] == True:
                 return 0
             minBuckets[toMinutes(time) - 1] = True
             
         last = None
         maxTime = 0
         minTime = 24 * 60 + 1
         minDistance = 24 * 60 + 1
         for i in range(24 * 60):
             if last == None and minBuckets[i] == True:
                 first = i
                 last = i
             elif minBuckets[i] == True:
                 minDistance = min(minDistance, i - last)
                 last = i
                 veryLast = i
         minDistance = min(minDistance, 24 * 60 - veryLast + first)
         return minDistance
         
         
                 
             

class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
 
         def get_minutes(time_point):
             hour, minute = list(map(int, time_point.split(':')))
             return 60 * hour + minute
 
         minutes = sorted(map(get_minutes, timePoints))
         minutes.append(60 * 24 + minutes[0])
         return min(y - x for x, y in zip(minutes, minutes[1:]))


class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         tlen = len(timePoints)
         if tlen < 2: return 0
         arr = [0] * tlen
         for i in range(tlen):
             h, m = timePoints[i].split(':')
             arr[i] = int(h) * 60 + int(m)
         arr.sort()
         minx = float('inf')
         for i in range(1, tlen):
             if minx > arr[i] - arr[i - 1]:
                 minx = arr[i] - arr[i - 1]
         if minx > arr[0] + 24 * 60 - arr[-1]:
             minx = arr[0] + 24 * 60 - arr[-1]
         return minx

# 17:06
 class Solution:
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
 
         t = [int(y[0])*60 + int(y[1]) for x in timePoints for y in [x.split(":")]]   ## HH * 60 + MM
         t = sorted(t)
 
         t.append(t[0]+24*60)
         print(t)
         best = float("inf")
         for i in range(len(timePoints)):
             best = min(best, (t[i+1]-t[i]))
         return best
 # 17:10
 # 17:16 be careful of line 9: ... for y in [ ... ]; otherwise we will be looking at one number at a time
 # 17:18 should not do MOD 12 hours
              

class Solution:
     def to_minutes(self, timePoints): # if in place
         ret_list = []
         for hour in timePoints:
             h, m = tuple(map(int, hour.split(':')))
             
             m += h*60
             ret_list.append(m)
         
         return ret_list
             
             
     def findMinDifference(self, timePoints):
         """
         :type timePoints: List[str]
         :rtype: int
         """
         minutes_p = self.to_minutes(timePoints)
         
         minutes_p.sort()
         
         min_minutes = 1439 # distance from 00:00 to 23:59
         for i in range(len(minutes_p)-1):
             if i == 0:
                 min_minutes = min(abs(minutes_p[i]-minutes_p[i-1]+1440),min_minutes)
             min_minutes = min(min(abs(minutes_p[i]-minutes_p[i+1]), abs(1440-abs(minutes_p[i]-minutes_p[i+1]))), min_minutes)
         
         print(min_minutes)
         return min_minutes