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.
"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.
"01:00"
is not valid. It should be "1:00"
.The minute must consist of two digits and may contain a leading zero.
"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
This problem asks to find all possible times on a binary watch given the number of LEDs that are currently on. The watch has 4 LEDs for hours (0-11) and 6 LEDs for minutes (0-59).
Both solutions presented leverage bit manipulation for efficiency. The core idea is to iterate through all possible combinations of LED states (0 or 1) and check if the number of LEDs on matches the input turnedOn
.
This solution uses nested loops to iterate through all possible hour and minute combinations. For each combination, it checks if the total number of LEDs on (calculated using bit counting) equals turnedOn
. If it does, the time is formatted and added to the result list.
Time Complexity: O(12 * 60) = O(720). The time complexity is constant because the number of possible hour and minute combinations is fixed (12 hours * 60 minutes).
Space Complexity: O(N), where N is the number of valid time combinations found. In the worst case, this could be all possible combinations, although this is unlikely.
This approach is more concise. It iterates through all possible 10-bit combinations (using a bitmask from 0 to 1023) representing the states of all 10 LEDs. Each 10-bit number is then split into hour and minute representations using bit shifting. Again, it checks the bit count against turnedOn
and formats the time accordingly.
Time Complexity: O(210) = O(1024). This iterates through all possible 10-bit combinations, which is a constant number.
Space Complexity: O(N), where N is the number of valid time combinations. Similar to solution 1, the space complexity depends on the output and is at most O(720) in worst case.
Both solutions are implemented in several languages, highlighting the similar logic and efficiency of both approaches across different programming languages. The differences mostly lie in syntax and built-in functions for bit manipulation and string formatting.
i.bit_count()
Integer.bitCount(i)
__builtin_popcount(i)
bits.OnesCount(uint(i))
Both solutions achieve the same result efficiently, but Solution 2, using a single loop and bit manipulation, is generally considered slightly more efficient due to the reduced number of iterations. The difference is minimal, however, because the number of possible combinations is relatively small. Solution 1 might be easier to understand for beginners, while solution 2 is more compact and elegant.