|
| 1 | +# [Problem 401: Binary Watch](https://leetcode.com/problems/binary-watch/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +This problem describes a small search space: hours use 4 LEDs (0-11) and minutes use 6 LEDs (0-59). We are given the total number of LEDs that are on and asked to return all valid times with exactly that many bits set. My first thought is to brute-force all possible hour/minute pairs (12 * 60 = 720 possibilities) and check how many bits are set in the combined representation (or separately in hour and minute and sum). That's trivial and very fast given the tiny fixed search space. |
| 5 | + |
| 6 | +Another idea is combinatorial: choose k bits among 10 positions and interpret which belong to hours vs minutes. That works but is more complex and unnecessary since brute force is constant-time for this input size. |
| 7 | + |
| 8 | +Edge cases: turnedOn = 0 (should return "0:00"). turnedOn > 10 yields no solutions (but the brute-force will just return empty). Minutes must be zero-padded to two digits; hours must not have leading zero. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +Refinements: |
| 12 | +- Validate hours < 12 and minutes < 60 while iterating. |
| 13 | +- Use Python's int.bit_count() (or bin(x).count('1')) to get number of set bits efficiently. |
| 14 | +- Complexity is constant (720 checks), so time/space are trivial. We should return times in any order. |
| 15 | + |
| 16 | +Alternative: |
| 17 | +- Use combination generation by splitting turnedOn into h_bits (0..min(4, turnedOn)) and m_bits = turnedOn - h_bits and generate hour values with h_bits ones and minute values with m_bits ones. That's slightly more structured but not necessary. |
| 18 | + |
| 19 | +Complexity: |
| 20 | +- Time: O(12 * 60) = O(1) practically, or O(720) operations. |
| 21 | +- Space: O(number of valid times) in the output, at most 720 but practically far smaller. |
| 22 | + |
| 23 | +## Attempted solution(s) |
| 24 | +```python |
| 25 | +from typing import List |
| 26 | + |
| 27 | +class Solution: |
| 28 | + def readBinaryWatch(self, turnedOn: int) -> List[str]: |
| 29 | + # If turnedOn is impossible (> 10), early return (optional) |
| 30 | + if turnedOn < 0 or turnedOn > 10: |
| 31 | + return [] |
| 32 | + |
| 33 | + res = [] |
| 34 | + for h in range(12): # hours: 0..11 |
| 35 | + for m in range(60): # minutes: 0..59 |
| 36 | + # Use int.bit_count() for efficient bit counting (Python 3.8+) |
| 37 | + if (h.bit_count() + m.bit_count()) == turnedOn: |
| 38 | + res.append(f"{h}:{m:02d}") |
| 39 | + return res |
| 40 | +``` |
| 41 | +- Notes: |
| 42 | + - Approach: brute-force iterate all 12 * 60 possible times and count the number of set bits in hour and minute; format minutes with two digits. |
| 43 | + - Complexity: Time O(720) = O(1) practically; Space O(R) where R is number of results returned. |
| 44 | + - Implementation detail: Using int.bit_count() is efficient; if unavailable, bin(x).count('1') can be used instead. The hour has no leading zero (string formatting of h as int achieves that), and minutes are zero-padded with :02d formatting. |
0 commit comments