A binary watch has 4 LEDs on the top to represent the hours (0-11), and the 6 LEDs on the bottom to represent the minutes (0-59). Each LED has a binary value (1, 2, 4, 8, 16, 32), and they are read by summing the values of the LEDs that are lit.
Given a non-negative integer num
which represents the number of LEDs that are currently on, return all possible times the watch could represent.
The order of output does not matter.
num
be larger than 10?
h
is hours (0-11) and mm
is minutes (00-59).num
be 0?
bin(x).count('1')
).def readBinaryWatch(num: int):
def bit_count(x):
return bin(x).count('1')
times = []
for h in range(12):
for m in range(60):
if bit_count(h) + bit_count(m) == num:
times.append(f"{h}:{m:02d}")
return times
# Example usage:
# num = 1
# print(readBinaryWatch(num))
O(1)
since the max number is 12 (hours) and 59 (minutes).O(720 * 2)
which simplifies to O(720)
or O(1)
since 720 is a constant.This approach provides an efficient and concise solution to determining all possible times for a given number of LEDs lit on a binary watch.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?