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. 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. The minute must consist of two digits and may contain a leading zero.
For example, if turnedOn = 1
, the output should be ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
.
If turnedOn = 9
, the output should be []
.
Constraints:
0 <= turnedOn <= 10
class Solution:
def readBinaryWatch(self, 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("%d:%02d" % (hour, minute))
return result
The most straightforward approach is to iterate through all possible hours (0-11) and minutes (0-59), and then count the number of set bits (1s) in the binary representation of each. If the sum of set bits equals turnedOn
, then we add the formatted time string to our result. This approach explores the entire time space, without any optimizations.
The code provided above already represents an efficient solution for the problem. Since the hour and minute ranges are relatively small (0-11 and 0-59, respectively), the time complexity remains manageable, and there's no need for further optimization. The key to this solution lies in its simplicity and clarity, directly addressing the problem's constraints without introducing complex data structures or algorithms.
bin(hour).count('1')
and bin(minute).count('1')
: The bin()
function converts integers to their binary representation (a string), and then count('1')
counts the number of 1s in the string. For hours (0-11), the maximum number of bits is 4, and for minutes (0-59), the maximum number of bits is 6. So, the count('1')
operation takes O(1) time since the number of bits is bounded by a small constant.Therefore, the overall time complexity is O(1), since all operations are bounded by constants.
result
list: The space used by the result
list depends on the number of valid times that can be formed with turnedOn
LEDs. In the worst case, with turnedOn = 0
, the result
list will contain one element ('0:00'). With turnedOn = 6
, the number of elements would still be relatively small. The space used by the strings is bounded by O(1), since we're just formatting "hourTherefore, the overall space complexity is O(1) as the size of the result
list is bounded by a constant.
0 <= turnedOn <= 10
. If turnedOn
is greater than 10, the loop won't find any valid combinations because the maximum number of LEDs is 4 (hours) + 6 (minutes) = 10.turnedOn
is less than 0, the loop still works as expected, but it won't find any valid combinations, as it's looking for a negative number of turned on LEDs.turnedOn
LEDs, the code returns an empty list. For example, if turnedOn = 9
or turnedOn = 10
, few or no valid combinations exist."%d:%02d"
, which uses %d
for hours, thus omitting any leading zeros."%d:%02d"
uses %02d
for minutes, which ensures that minutes are always displayed with two digits, padding with a leading zero if necessary.