Post

539 Minimum Time Difference

539 Minimum Time Difference

Minimum Time Difference imageGiven 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.

 

Example 1:

1
2
3
**Input:** timePoints = ["23:59","00:00"]
**Output:** 1

Example 2:

1
2
3
**Input:** timePoints = ["00:00","23:59","00:00"]
**Output:** 0

 

Constraints:

1
2
2 <= timePoints.length <= 2 * 104
timePoints[i] is in the format **"HH:MM"**.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

class Solution:
    def findMinDifference(self, timePoints: List[str]) -> int:
        result = []
        for k in timePoints:
            hour = k.split(":")[0]
            minute = k.split(":")[1]
            total = int(hour) * 60 + int(minute)
            result.append(total)
        
        result.sort()
        diff = 1440
        start = 1
        end = len(result)
        while(start < end):
            diff = min(diff, result[start] - result[start-1])
            start += 1
        
        diff = min(diff, result[0] + 1440 - result[-1])
        return diff

        



This post is licensed under CC BY 4.0 by the author.