Skip to content

Latest commit

 

History

History
106 lines (73 loc) · 3.53 KB

401.binary-watch.en.md

File metadata and controls

106 lines (73 loc) · 3.53 KB

Problem (401. (Watch)

https://leetcode.com/problems/binary-watch/

Title description

The binary watch has 4 LEDS on the top to represent the hour (0-11), and the 6 LEDs on the bottom to represent the minute (0-59).

Each LED represents a 0 or 1, and the lowest position is on the right.

For example, the binary watch above reads “3:25”.

Given a non-negative integer n that represents the number of CURRENT LEDs on, all possible times are returned.



example:

Input: n = 1
return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]


prompt:

There is no requirement for the order of output.
The hour will not start with zero. For example, “01:00” is not allowed and should be “1:00”.
Minutes must be composed of two digits and may start with zero. For example, “10:2” is invalid and should be “10:02”.
Data that exceeds the stated range (hours 0-11, minutes 0-59) will be discarded, which means it will not appear "13:00", "0:61" Wait for time.

Source: LeetCode
Link:https://leetcode-cn.com/problems/binary-watch
The copyright belongs to the Link network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Pre-knowledge

-Cartesian product -backtracking

Company

-Ali -Tencent -Baidu -Byte

Idea

At first glance, the topic is a Cartesian product problem.

That is, to give you a number num, I can divide it into two parts. One part (which may as well be set to a) is given hours, and the other part is given points (which is num-a). The final result is the Cartesian product of the set of all hours that a can represent and the set of minutes that num-a can represent.

It is expressed in code:

# Enumerate hours
for a in possible_number(i):
# The hour is determined, the minute is num-i
for b in possible_number(num - i, True):
ans. add(str(a) + ":" + str(b). rjust(2, '0'))

Just enumerate all possible (a, num-a) combinations.

Core code:

for i in range(min(4, num + 1)):
for a in possible_number(i):
for b in possible_number(num - i, True):
ans. add(str(a) + ":" + str(b). rjust(2, '0'))

Code

class Solution:
def readBinaryWatch(self, num: int) -> List[str]:
def possible_number(count, minute=False):
if count == 0: return [0]
if minute:
return filter(lambda a: a < 60, map(sum, combinations([1, 2, 4, 8, 16, 32], count)))
return filter(lambda a: a < 12, map(sum, combinations([1, 2, 4, 8], count)))
ans = set()
for i in range(min(4, num + 1)):
for a in possible_number(i):
for b in possible_number(num - i, True):
ans. add(str(a) + ":" + str(b). rjust(2, '0'))
return list(ans)

Thinking further, in fact, what we are looking for is that the sum of a and b is equal to num, and a and b are the number of 1s in the binary representation. Therefore, the logic can be simplified to:

class Solution:
def readBinaryWatch(self, num: int) -> List[str]:
return [str(a) + ":" + str(b). rjust(2, '0') for a in range(12) for b in range(60) if (bin(a)+bin(b)). count('1') == num]

If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm.