Python O(1) solution beats 99%


  • 0
    S

    There are only 1440 times. Therefore put each time's occurence into a hashtable, if a same time appear twice than return 0, else sort the key of the map, find time difference between adjacent elements, then find the minimum of the time difference

    class Solution(object):
        def findMinDifference(self, timePoints):
            """
            :type timePoints: List[str]
            :rtype: int
            """
            time=collections.defaultdict(int)
            for t in timePoints:
                hour=int(t[0:2])
                minute=int(t[-2:])
                time[hour*60+minute]+=1
                if time[hour*60+minute]>1:
                    return 0
            timeList=sorted(time.keys())
            deltaM=[timeList[i+1]-timeList[i] for i in range(0, len(timeList)-1)]
            deltaM.append(1440-timeList[-1]+timeList[0])
            return min(deltaM)
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.