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.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Sorting

Data structures

Mathematics

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

Solutions:
class Solution:

	def findMinDifference(self, timePoints):
		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):
		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


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):
		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):
		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):
		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):
		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):

		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


class Solution:

	def findMinDifference(self, timePoints):

		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):
		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


class Solution:

	def to_minutes(self, timePoints):
		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):
		minutes_p = self.to_minutes(timePoints)
		minutes_p.sort()
		min_minutes = 1439
		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