python3 beat 100%


  • 0
    J

    idea is to compute the dictionary for hour and minutes first

    class Solution:
        def readBinaryWatch(self, num):
            """
            :type num: int
            :rtype: List[str]
            """
            dic1={0:[0],1:[1,2,4,8],2:[3,5,9,6,10],3:[7,11]} #hour is easy to write
            lst2=[1,2,4,8,16,32]
            dic2={} #calculate minute dictionary
            
            for i in range(7):
                dic2[i]=[]
                lst1=self.convert(lst2,i)
                for j in lst1:
                    if j<60:
                        dic2[i].append(j)
    
            res=[]  #now adding time to result
            for i in range(min(num,3)+1):
                for hr in dic1[i]:
                    if num-i<7:
                        for minute in dic2[num-i]:
                            time=str(hr)+':'
                            if minute<10:
                                time+='0'+str(minute)
                            else:
                                time+=str(minute)
                            res.append(time)
            return res
            
        def convert(self,lst,n):  #convert to possible minutes
            if n==0: return [0]
            if n==1: return lst
            if len(lst)<n: return []
            else: return self.convert(lst[1:],n)+[lst[0]+i for i in self.convert(lst[1:],n-1)]
    

Log in to reply
 

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