Binary Watch

Easy
19 days ago

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

Sample Answer
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

Naive Solution

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.

Optimal Solution

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.

Big(O) Run-time Analysis

  • Outer Loop (Hour): Iterates from 0 to 11, which is a constant 12 iterations.
  • Inner Loop (Minute): Iterates from 0 to 59, which is a constant 60 iterations.
  • 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.

Big(O) Space Usage Analysis

  • 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 "hour", with a small number of digits.

Therefore, the overall space complexity is O(1) as the size of the result list is bounded by a constant.

Edge Cases

  1. turnedOn > 10: The problem states that 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.
  2. turnedOn < 0: If 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.
  3. No possible times: If there are no possible times for the given number of turnedOn LEDs, the code returns an empty list. For example, if turnedOn = 9 or turnedOn = 10, few or no valid combinations exist.
  4. Hour or minute exceeding limits: The code inherently handles the limits by iterating from 0 to 11 for hours and 0 to 59 for minutes, preventing any out-of-bounds times.
  5. Leading zero in hours: The problem specifies that the hour must not contain a leading zero. This is automatically handled by the formatting string "%d:%02d", which uses %d for hours, thus omitting any leading zeros.
  6. Minute formatting: The problem specifies that the minute must consist of two digits, possibly with a leading zero. The formatting string "%d:%02d" uses %02d for minutes, which ensures that minutes are always displayed with two digits, padding with a leading zero if necessary.