Sorta-like a Two Sum (Python 60ms)

  • 0

    Once you figure out how many ones are in the binary representation of each possible hour and and each possible minute. It just becomes a simple problem of finding all combinations that add up to the available number of 1s and then formatting the output correctly.

    def readBinaryWatch(self, num):
            :type num: int
            :rtype: List[str]
            #encode time in 10 digits
            # 4 high 0 - 11
            # 6 low 0-59
            def getOnes(n):
            	c = 0
            	while n!= 0:
            		c += n & 1
            		n = n >> 1
            	return c 
            mMap = {i:getOnes(i) for i in range(60)}
            hMap = {i:getOnes(i) for i in range(12)}
        	toRet = []
        	for hour in filter(lambda x: hMap[x] <= num, range(12)):
        		nOnes = hMap[hour]
        		toRet += ["{}:{:02d}".format(hour, minn) for minn in filter(lambda x: mMap[x] == num - nOnes, range(60)) ]
            return toRet

  • 0

    You can use bin() function and count() function to reduce code even further.

Log in to reply

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