Skip to the content.

leetcode [539] 最小时间差


Contact me:

Blog -> https://cugtyt.github.io/blog/index
Email -> cugtyt@qq.com
GitHub -> Cugtyt@GitHub


给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示。

示例 1:

输入: ["23:59","00:00"]
输出: 1

备注:

列表中时间数在 2~20000 之间。
每个时间取值在 00:00~23:59 之间。
class Solution:
    def findMinDifference(self, timePoints: List[str]) -> int:
        def conv(time):
            time = time.split(':')
            return int(time[0]) * 60 + int(time[1])
        timePoints = [conv(tp) for tp in timePoints]
        timePoints.sort()
        res = 24*60
        for i in range(len(timePoints) - 1):
            res = min(res, timePoints[i + 1] - timePoints[i])
        res = min(res, timePoints[0] + 24 * 60 - timePoints[-1])
        return res