A clear explanation of the Binary Watch problem using bit counting over all valid times.
Problem Restatement
A binary watch uses LEDs to show time.
There are 4 LEDs for the hour.
The hour range is:
0 <= hour <= 11There are 6 LEDs for the minute.
The minute range is:
0 <= minute <= 59Each LED represents one binary bit. A bit with value 1 means the LED is on.
Given an integer turnedOn, return all possible valid times where exactly turnedOn LEDs are on.
The answer may be returned in any order.
Input and Output
| Item | Meaning |
|---|---|
| Input | An integer turnedOn |
| Output | A list of time strings |
| Hour range | 0 to 11 |
| Minute range | 0 to 59 |
| Format | "H:MM" |
The hour has no leading zero.
The minute must always have two digits.
So:
1:05is valid, but:
01:5is not valid.
Examples
For:
turnedOn = 1Only one LED is on.
Possible times include:
"1:00"
"2:00"
"4:00"
"8:00"
"0:01"
"0:02"
"0:04"
"0:08"
"0:16"
"0:32"Each of these times uses exactly one binary 1.
For example:
4:00The hour 4 in binary is:
0100So one hour LED is on.
The minute 0 in binary is:
000000So no minute LED is on.
Total LEDs on:
1 + 0 = 1First Thought: Generate LED Combinations
One way is to choose which LEDs are on.
There are 10 LEDs total:
4 hour LEDs + 6 minute LEDs = 10 LEDsWe could generate all combinations of turnedOn LEDs, compute the hour and minute, then keep only valid times.
This works, but it needs more bookkeeping.
There is a simpler way.
Key Insight
There are only 12 possible hours and 60 possible minutes.
So there are only:
12 * 60 = 720possible valid times.
We can simply check every valid time.
For each pair (hour, minute), count how many 1 bits appear in:
hourand:
minuteIf the total number of 1 bits equals turnedOn, then this time is part of the answer.
Algorithm
Create an empty list ans.
Loop through every valid hour:
0 <= hour < 12Loop through every valid minute:
0 <= minute < 60For each time:
- Count the number of
1bits inhour. - Count the number of
1bits inminute. - Add the two counts.
- If the total equals
turnedOn, format the time as"H:MM"and append it.
Correctness
Every valid binary watch time must have an hour between 0 and 11 and a minute between 0 and 59.
The algorithm checks every such pair.
For each checked pair, it computes exactly how many LEDs are on by counting the 1 bits in the hour and minute. This matches the watch definition, because each LED corresponds to one binary bit.
If the count equals turnedOn, the time satisfies the requirement and is added to the result.
If the count differs from turnedOn, the time cannot be represented with exactly that many LEDs, so it is skipped.
Because the algorithm checks all valid times and keeps exactly the times with the required number of lit LEDs, the returned list is correct.
Complexity
| Metric | Value | Why |
|---|---|---|
| Time | O(12 * 60) | We check every valid time |
| Space | O(1) extra | Apart from the output list |
Since 12 * 60 = 720, this is effectively constant time.
Implementation
class Solution:
def readBinaryWatch(self, turnedOn: int) -> list[str]:
ans = []
for hour in range(12):
for minute in range(60):
lights = hour.bit_count() + minute.bit_count()
if lights == turnedOn:
ans.append(f"{hour}:{minute:02d}")
return ansCode Explanation
We store valid times in:
ans = []Then we enumerate every possible hour:
for hour in range(12):This gives hours from 0 through 11.
For each hour, we enumerate every possible minute:
for minute in range(60):This gives minutes from 0 through 59.
Then we count the number of turned-on LEDs:
lights = hour.bit_count() + minute.bit_count()The method bit_count() returns the number of 1 bits in the binary representation of an integer.
For example:
5.bit_count()returns 2, because 5 is binary 101.
If the total number of lights matches turnedOn, we add the formatted time:
ans.append(f"{hour}:{minute:02d}")The format rule matters.
hour is printed normally.
minute:02d means the minute must use two digits.
So:
5becomes:
05Finally, we return all valid times:
return ansTesting
def test_binary_watch():
s = Solution()
assert sorted(s.readBinaryWatch(0)) == ["0:00"]
assert sorted(s.readBinaryWatch(1)) == sorted([
"1:00", "2:00", "4:00", "8:00",
"0:01", "0:02", "0:04", "0:08", "0:16", "0:32",
])
assert "3:00" in s.readBinaryWatch(2)
assert "0:03" in s.readBinaryWatch(2)
assert "10:00" in s.readBinaryWatch(2)
assert all(
0 <= int(t.split(":")[0]) <= 11 and
0 <= int(t.split(":")[1]) <= 59
for t in s.readBinaryWatch(5)
)
assert s.readBinaryWatch(11) == []
print("all tests passed")Test Notes
| Test | Why |
|---|---|
turnedOn = 0 | Only 0:00 is possible |
turnedOn = 1 | Checks the sample-style simple case |
turnedOn = 2 | Checks hour-only, minute-only, and mixed binary values |
turnedOn = 5 | Checks that all returned times are valid |
turnedOn = 11 | No valid time can use 11 LEDs because the largest valid hour and minute do not turn on all 10 useful bits at once |