Minimum Time Difference

Problem Id: 539 Difficulty: Medium


Intuition

Solution


class Solution(object):
    def findMinDifference(self, timePoints):
        """
        :type timePoints: List[str]
        :rtype: int
        """
        timePoints = [s.split(':') for s in timePoints]
        timePoints = [int(x) * 60 + int(y) for x, y in timePoints]
        timePoints.sort()
        ans = timePoints[1] - timePoints[0]
        for i in range(len(timePoints)):
            x = timePoints[i]
            y = timePoints[(i + 1) % len(timePoints)]
            if x > y:
                y += 24 * 60
            ans = min(ans, y - x)
        return ans