|
| 1 | +--- |
| 2 | +id: binary-watch |
| 3 | +title: Binary Watch |
| 4 | +sidebar_label: 401-Binary Watch |
| 5 | +tags: |
| 6 | + - Backtracking |
| 7 | + - Bit Manipulation |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Binary Watch problem on LeetCode." |
| 13 | +sidebar_position: 3 |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +The hour must not contain a leading zero. |
| 23 | + |
| 24 | +For example, "01:00" is not valid. It should be "1:00". |
| 25 | +The minute must consist of two digits and may contain a leading zero. |
| 26 | + |
| 27 | +For example, "10:2" is not valid. It should be "10:02". |
| 28 | + |
| 29 | +### Examples |
| 30 | + |
| 31 | +**Example 1:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: turnedOn = 1 |
| 35 | +Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"] |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 2:** |
| 39 | + |
| 40 | +``` |
| 41 | +Input: turnedOn = 9 |
| 42 | +Output: [] |
| 43 | +``` |
| 44 | + |
| 45 | +### Constraints |
| 46 | + |
| 47 | +- `0 <= turnedOn <= 10` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Solution for Binary Watch Problem |
| 52 | + |
| 53 | +### Approach: Bit Manipulation and Backtracking |
| 54 | + |
| 55 | +The problem can be solved using bit manipulation and backtracking. We iterate through all possible hour and minute values, count the number of `1`s in their binary representations, and check if it matches the number of LEDs turned on. |
| 56 | + |
| 57 | +#### Code in Different Languages |
| 58 | + |
| 59 | +<Tabs> |
| 60 | +<TabItem value="C++" label="C++" default> |
| 61 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 62 | + |
| 63 | +```cpp |
| 64 | +#include <vector> |
| 65 | +#include <string> |
| 66 | +#include <bitset> |
| 67 | + |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + std::vector<std::string> readBinaryWatch(int turnedOn) { |
| 71 | + std::vector<std::string> times; |
| 72 | + for (int h = 0; h < 12; ++h) { |
| 73 | + for (int m = 0; m < 60; ++m) { |
| 74 | + if (std::bitset<10>((h << 6) + m).count() == turnedOn) { |
| 75 | + times.push_back(std::to_string(h) + (m < 10 ? ":0" : ":") + std::to_string(m)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return times; |
| 80 | + } |
| 81 | +}; |
| 82 | +``` |
| 83 | +
|
| 84 | +</TabItem> |
| 85 | +<TabItem value="Java" label="Java"> |
| 86 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 87 | +
|
| 88 | +```java |
| 89 | +import java.util.ArrayList; |
| 90 | +import java.util.List; |
| 91 | +
|
| 92 | +class Solution { |
| 93 | + public List<String> readBinaryWatch(int turnedOn) { |
| 94 | + List<String> times = new ArrayList<>(); |
| 95 | + for (int h = 0; h < 12; h++) { |
| 96 | + for (int m = 0; m < 60; m++) { |
| 97 | + if (Integer.bitCount((h << 6) + m) == turnedOn) { |
| 98 | + times.add(String.format("%d:%02d", h, m)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return times; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +</TabItem> |
| 108 | +<TabItem value="Python" label="Python"> |
| 109 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 110 | + |
| 111 | +```python |
| 112 | +class Solution: |
| 113 | + def readBinaryWatch(self, turnedOn: int) -> List[str]: |
| 114 | + times = [] |
| 115 | + for h in range(12): |
| 116 | + for m in range(60): |
| 117 | + if bin(h).count('1') + bin(m).count('1') == turnedOn: |
| 118 | + times.append(f"{h}:{m:02d}") |
| 119 | + return times |
| 120 | +``` |
| 121 | + |
| 122 | +</TabItem> |
| 123 | +</Tabs> |
| 124 | + |
| 125 | +#### Complexity Analysis |
| 126 | + |
| 127 | +- **Time Complexity**: $O(1)$, since the number of possible combinations (12 hours * 60 minutes) is constant. |
| 128 | +- **Space Complexity**: $O(1)$, not counting the space required for the output list. |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +<h2>Authors:</h2> |
| 133 | + |
| 134 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 135 | +{['ImmidiSivani'].map(username => ( |
| 136 | + <Author key={username} username={username} /> |
| 137 | +))} |
| 138 | +</div> |
0 commit comments