539. Minimum Time Difference

https://leetcode.com/contest/leetcode-weekly-contest-23/problems/minimum-time-difference/

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.

1
2
3
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.

my code:

正常思路,就可以解决问题,问题在于要思考到第一个和最后一个的距离,这个不可以忽略。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def findMinDifference(self, timePoints):
"""
:type timePoints: List[str]
:rtype: int
"""
if len(timePoints)==0 or len(timePoints)==1: return 0
tmp = []
for t in timePoints:
tt = t.split(':')
tt = int(tt[0])*60+int(tt[1])
tmp.append(tt)
tmp.sort()
res = min(abs(tmp[0]-tmp[-1]), 1440-abs(tmp[0]-tmp[-1]))
for i in range(1, len(tmp)):
cur = min(abs(tmp[i]-tmp[i-1]), 1440-abs(tmp[i]-tmp[i-1]))
res = min(cur, res)
return res

谢谢你,可爱的朋友。