python O(n) bucket sort


  • 0
    def findMinDifference(self, timePoints):
        t = [0] * 24 * 60         #array for the time. they are buckets
        for s in timePoints:
            v = int(s[:2]) * 60 + int(s[3:5])
            if t[v]==1: return 0  #find duplicate time
            t[v] = 1
        tt = t + t               # concat two time arrays together, to handle cases like [23:59, 00:01]
        ans = 24*60        # value to return
        prev = -24*60
        for i in xrange(len(tt)):
            if tt[i]:
                ans = min(ans, i-prev)
                prev = i
        return ans

Log in to reply
 

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