|
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +class Solution: |
| 5 | + """Base class for all LeetCode Problems.""" |
| 6 | + |
| 7 | + def readBinaryWatch(self, turnedOn: int) -> List[str]: |
| 8 | + """ |
| 9 | + A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs |
| 10 | + on the bottom to represent the minutes (0-59). Each LED represents a zero or |
| 11 | + one, with the least significant bit on the right. |
| 12 | + - For example, the below binary watch reads "4:51". |
| 13 | + |
| 14 | + Given an integer turnedOn which represents the number of LEDs that are currently |
| 15 | + on (ignoring the PM), return all possible times the watch could represent. You |
| 16 | + may return the answer in any order. |
| 17 | + |
| 18 | + The hour must not contain a leading zero. |
| 19 | + - For example, "01:00" is not valid. It should be "1:00". |
| 20 | + |
| 21 | + The minute must consist of two digits and may contain a leading zero. |
| 22 | + - For example, "10:2" is not valid. It should be "10:02". |
| 23 | + """ |
| 24 | + if 0 < turnedOn >= 9: |
| 25 | + return [] |
| 26 | + |
| 27 | + queue = [(0, 0, 0, {8, 4, 2, 1}, {32, 16, 8, 4, 2, 1})] |
| 28 | + visited = set() |
| 29 | + result = [] |
| 30 | + while queue: |
| 31 | + # Get data from queue |
| 32 | + ( |
| 33 | + cur_hour, |
| 34 | + cur_minutes, |
| 35 | + cur_turnedOn, |
| 36 | + cur_hour_missing, |
| 37 | + cur_minutes_missing, |
| 38 | + ) = queue.pop() |
| 39 | + |
| 40 | + # Check if goal state is reached |
| 41 | + if turnedOn == cur_turnedOn: |
| 42 | + result.append((cur_hour, cur_minutes)) |
| 43 | + continue |
| 44 | + |
| 45 | + # Iterate over all valid hour options |
| 46 | + for hour in cur_hour_missing: |
| 47 | + # Next state |
| 48 | + next_hour = cur_hour + hour |
| 49 | + next_minutes = cur_minutes |
| 50 | + next_turnedOn = cur_turnedOn + 1 |
| 51 | + next_hour_missing = cur_hour_missing - {hour} |
| 52 | + next_minutes_missing = cur_minutes_missing |
| 53 | + |
| 54 | + # Check if next time is valid and is not visited before |
| 55 | + if next_hour <= 11 and (next_hour, next_minutes) not in visited: |
| 56 | + visited.add((next_hour, next_minutes)) |
| 57 | + queue.append( |
| 58 | + ( |
| 59 | + next_hour, |
| 60 | + next_minutes, |
| 61 | + next_turnedOn, |
| 62 | + next_hour_missing, |
| 63 | + next_minutes_missing, |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + # Iterate over all valid minutes options |
| 68 | + for minutes in cur_minutes_missing: |
| 69 | + # Next state |
| 70 | + next_hour = cur_hour |
| 71 | + next_minutes = cur_minutes + minutes |
| 72 | + next_turnedOn = cur_turnedOn + 1 |
| 73 | + next_hour_missing = cur_hour_missing |
| 74 | + next_minutes_missing = cur_minutes_missing - {minutes} |
| 75 | + |
| 76 | + # Check if next time is valid and is not visited before |
| 77 | + if next_minutes <= 59 and (next_hour, next_minutes) not in visited: |
| 78 | + visited.add((next_hour, next_minutes)) |
| 79 | + queue.append( |
| 80 | + ( |
| 81 | + next_hour, |
| 82 | + next_minutes, |
| 83 | + next_turnedOn, |
| 84 | + next_hour_missing, |
| 85 | + next_minutes_missing, |
| 86 | + ) |
| 87 | + ) |
| 88 | + return list(map(lambda item: f"{item[0]}:{item[1]:02d}", result)) |
0 commit comments