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 <= 10When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're trying to find all possible times that can be represented on a binary watch, given a certain number of lights are on. The brute force method explores every single combination of lights to see if it represents a valid time. We simply check all possibilities.
Here's how the algorithm would work step-by-step:
def binary_watch(turned_on_lights):
possible_times = []
for hour in range(12):
for minute in range(60):
# Count set bits to find valid times.
if (bin(hour).count('1') + bin(minute).count('1')) == turned_on_lights:
possible_times.append("%d:%02d" % (hour, minute))
return possible_timesThe binary watch puzzle involves figuring out how many combinations of lights on the watch add up to a specific number. The fastest approach is to realize we can systematically check all possible combinations of bits allocated to hours and minutes, rather than generating and filtering.
Here's how the algorithm would work step-by-step:
def read_binary_watch(turned_on):
time_combinations = []
# Iterate through all possible hour values
for hour_value in range(12):
# Iterate through all possible minute values
for minute_value in range(60):
# Count the total number of set bits (1s)
total_bits_set = bin(hour_value).count('1') + bin(minute_value).count('1')
# Check if the total matches the target
if total_bits_set == turned_on:
# Format the valid time as 'H:MM'
time_combinations.append("%d:%02d" % (hour_value, minute_value))
return time_combinations
def main():
number_of_lights_on = 1
possible_times = read_binary_watch(number_of_lights_on)
print(f"Possible times with {number_of_lights_on} lights on: {possible_times}")
number_of_lights_on = 2
possible_times = read_binary_watch(number_of_lights_on)
print(f"Possible times with {number_of_lights_on} lights on: {possible_times}")
# Edge case: all lights off
number_of_lights_on = 0
possible_times = read_binary_watch(number_of_lights_on)
print(f"Possible times with {number_of_lights_on} lights on: {possible_times}")
# Edge case: all lights on, this will return empty array as its not possible within the
# constrains
number_of_lights_on = 10
possible_times = read_binary_watch(number_of_lights_on)
print(f"Possible times with {number_of_lights_on} lights on: {possible_times}")
if __name__ == "__main__":
main()| Case | How to Handle |
|---|---|
| Input number of turned on LEDs 'n' is negative | Return an empty list or throw an IllegalArgumentException as the number of LEDs cannot be negative. |
| Input number of turned on LEDs 'n' is greater than 10 | Return an empty list or throw an IllegalArgumentException as the maximum number of LEDs is 10. |
| n = 0 (no LEDs turned on) | This should return '0:00' as the only valid time. |
| n = 10 (all LEDs turned on) | This will result in the time '19:15' and needs to be properly calculated and formatted. |
| Combinations that result in invalid hours (hour > 11) | Skip combinations that result in hours greater than 11 during the time calculation. |
| Combinations that result in invalid minutes (minutes > 59) | Skip combinations that result in minutes greater than 59 during the time calculation. |
| Combinations that result in valid times but hour or minutes are single digit. | Pad the minutes with a leading zero if it is less than 10 to maintain the 'X:XX' format. |
| Multiple combinations result in the same time. | Since we calculate based on the number of LEDs, avoid duplicates, typically the generation inherently handles this, but should be checked. |