Python O(1) code


  • 0
    M

    Checking each digits if there are bigger than itself.

    class Solution:
        
        def print_res(self, a, b, c, d):
            return str(a) + str(b) + ":" + str(c) + str(d)
        
        def nextClosestTime(self, time):
            a, b, c, d = int(time[0]), int(time[1]), int(time[3]), int(time[4])
            res = []
            t = (a, b, c, d)
            for e in t:
                if d < e <= 9:
                    res.append(e)
            if res:
                d = min(res)
                return self.print_res(a, b, c, d)
            
            for e in t:
                if c < e <= 5:
                    res.append(e)
            if res:
                d = min([a, b, c, d])
                c = min(res)
                return self.print_res(a, b, c, d)
            
            for e in t:
                if (b < e <= 3 and a == 2) or (b < e <= 9 and a < 2):
                    res.append(e)
            if res:
                d = min([a, b, c, d])
                c = min([x for x in (a, b, c, d) if x < 6])
                b = min(res)
                return self.print_res(a, b, c, d)
            
            return self.print_res(a, a, a, a)
    

Log in to reply
 

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