难度: Medium
原题连接
内容描述
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.
思路 1 - 时间复杂度: O(N^2)- 空间复杂度: O(N)******
先全部转换成分钟数,再排序,然后取最小差值,唯一要注意首尾两者差值可能还最小,例如["23:59","00:00"]
class Solution(object):
def findMinDifference(self, timePoints):
"""
:type timePoints: List[str]
:rtype: int
"""
tp = sorted(60*int(pt[:2])+int(pt[3:]) for pt in timePoints)
return min(min(b - a for (a, b) in zip(tp, tp[1:])), (24*60)+tp[0]-tp[-1])