leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

min_time_difference.py (781B)


# 539. Minimum Time Difference

"""
given a list of 24 hour clock time points in 'HH:MM' format, return the
minimum minutes difference between any two time points in the list
"""


class Solution(object):
    def findMinDifference(self, timePoints):
        """
        :type timePoints: List[str]
        :rtype: int
        """

        def convert(time):
            return int(time[:2]) * 60 + int(time[3:])

        minutes = map(convert, timePoints)
        minutes.sort()

        return min(
            (y - x) % (24 * 60) for x, y in zip(minutes, minutes[1:] + minutes[:1])
        )


if __name__ == "__main__":
    obj = Solution()
    print(obj.findMinDifference(timePoints=["23:59", "00:00"]))
    print(obj.findMinDifference(timePoints=["00:00", "23:59", "00:00"]))