A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
For example, the below binary watch reads "4:51".
Given an integer turnedOn
which represents the number of LEDs that are currently on (ignoring the PM), return all possible times the watch could represent. You may return the answer in any order.
The hour must not contain a leading zero.
For example, "01:00" is not valid. It should be "1:00".
The minute must consist of two digits and may contain a leading zero.
For example, "10:2" is not valid. It should be "10:02".
Example 1:
Input: turnedOn = 1 Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
Example 2:
Input: turnedOn = 9 Output: []
Constraints:
0 <= turnedOn <= 10
The most straightforward approach is to iterate through all possible hour and minute combinations and check if the number of set bits (1s) in their binary representations equals the given turnedOn
value.
turnedOn
, format the hour and minute into a string "Hdef readBinaryWatch_naive(turnedOn: int) -> list[str]:
result = []
for hour in range(12):
for minute in range(60):
if bin(hour).count('1') + bin(minute).count('1') == turnedOn:
result.append(f"{hour}:{minute:02}")
return result
This is essentially the same as the brute force solution since it's hard to optimize much further.
turnedOn
is less than 0 or greater than 10. If turnedOn
is greater than 10, there are no possible combinations.def count_bits(n: int) -> int:
count = 0
while n > 0:
n &= (n - 1)
count += 1
return count
def readBinaryWatch(turnedOn: int) -> list[str]:
result = []
for hour in range(12):
for minute in range(60):
if count_bits(hour) + count_bits(minute) == turnedOn:
result.append(f"{hour}:{minute:02}")
return result