From a43f13490a349027eefaf7009044694d455d99b0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 17 Feb 2026 03:20:33 +0000 Subject: [PATCH] feat: add solution for 0401. Binary Watch --- problems/0401-binary-watch/analysis.md | 54 +++++++++++++ problems/0401-binary-watch/problem.md | 62 +++++++++++++++ .../solution_daily_20260217.go | 20 +++++ .../solution_daily_20260217_test.go | 76 +++++++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 problems/0401-binary-watch/analysis.md create mode 100644 problems/0401-binary-watch/problem.md create mode 100644 problems/0401-binary-watch/solution_daily_20260217.go create mode 100644 problems/0401-binary-watch/solution_daily_20260217_test.go diff --git a/problems/0401-binary-watch/analysis.md b/problems/0401-binary-watch/analysis.md new file mode 100644 index 0000000..db55c10 --- /dev/null +++ b/problems/0401-binary-watch/analysis.md @@ -0,0 +1,54 @@ +# 0401. Binary Watch + +[LeetCode Link](https://leetcode.com/problems/binary-watch/) + +Difficulty: Easy +Topics: Backtracking, Bit Manipulation +Acceptance Rate: 58.9% + +## Hints + +### Hint 1 + +Think about what the 10 LEDs on the watch really represent. There are only 4 bits for hours and 6 bits for minutes — the total search space is quite small. Can you enumerate all possible hour and minute values and check which ones are valid? + +### Hint 2 + +Instead of thinking about choosing which LEDs to turn on (a combinatorial problem), consider iterating over every possible time (0:00 to 11:59) and counting how many bits are set in the hour and minute values combined. If that count equals `turnedOn`, the time is valid. + +### Hint 3 + +Use bit counting: for each hour `h` in [0, 11] and each minute `m` in [0, 59], compute `bits.OnesCount(h) + bits.OnesCount(m)`. If the sum equals `turnedOn`, format the time as `"h:mm"` and add it to your result. This avoids any complex backtracking and runs in constant time since the search space is fixed. + +## Approach + +The key insight is that the problem has a **fixed, tiny search space**. A binary watch can only display hours 0–11 and minutes 0–59. That's at most 12 × 60 = 720 possible times. + +Rather than using backtracking to choose which of the 10 LEDs to turn on (which is a valid but more complex approach), we can simply: + +1. Iterate over all valid hours (0 to 11). +2. For each hour, iterate over all valid minutes (0 to 59). +3. Count the number of 1-bits in the binary representation of both the hour and the minute. +4. If the total number of set bits equals `turnedOn`, format the time string and add it to the result. + +For formatting, the hour has no leading zero (just `%d`), while the minute is always two digits (`%02d`). + +**Example walkthrough with turnedOn = 1:** +- h=0, m=1 → bits(0)+bits(1) = 0+1 = 1 ✓ → "0:01" +- h=0, m=2 → bits(0)+bits(2) = 0+1 = 1 ✓ → "0:02" +- h=1, m=0 → bits(1)+bits(0) = 1+0 = 1 ✓ → "1:00" +- h=3, m=0 → bits(3)+bits(0) = 2+0 = 2 ✗ + +This naturally produces all valid times in sorted order (hours ascending, then minutes ascending). + +## Complexity Analysis + +Time Complexity: O(1) — We always check exactly 12 × 60 = 720 possible times, regardless of input. Bit counting is O(1) for small fixed-size integers. + +Space Complexity: O(1) — Excluding the output list, we only use a constant amount of extra space. + +## Edge Cases + +- **turnedOn = 0**: Only one valid time: `"0:00"`. Both hour and minute have zero bits set. +- **turnedOn >= 9**: No valid time exists. The maximum bits possible is 4 (hour=11, binary 1011) + 6 (minute=59, binary 111011) = 8 set bits total when hour=11 and minute=59, but that's only 8. Actually max is when h=11(3 bits) and m=59(5 bits) = 8 bits. So turnedOn=9 or 10 returns an empty list. +- **Large turnedOn values (e.g., 10)**: Return empty — impossible to have that many LEDs on with valid hour/minute ranges. diff --git a/problems/0401-binary-watch/problem.md b/problems/0401-binary-watch/problem.md new file mode 100644 index 0000000..4f31c72 --- /dev/null +++ b/problems/0401-binary-watch/problem.md @@ -0,0 +1,62 @@ +--- +number: "0401" +frontend_id: "401" +title: "Binary Watch" +slug: "binary-watch" +difficulty: "Easy" +topics: + - "Backtracking" + - "Bit Manipulation" +acceptance_rate: 5891.3 +is_premium: false +created_at: "2026-02-17T03:18:56.050706+00:00" +fetched_at: "2026-02-17T03:18:56.050706+00:00" +link: "https://leetcode.com/problems/binary-watch/" +date: "2026-02-17" +--- + +# 0401. Binary Watch + +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. + + * For example, the below binary watch reads `"4:51"`. + + + +![](https://assets.leetcode.com/uploads/2021/04/08/binarywatch.jpg) + +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. + + * For example, `"01:00"` is not valid. It should be `"1:00"`. + + + +The minute must consist of two digits and may contain a leading zero. + + * For example, `"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` diff --git a/problems/0401-binary-watch/solution_daily_20260217.go b/problems/0401-binary-watch/solution_daily_20260217.go new file mode 100644 index 0000000..3bc5776 --- /dev/null +++ b/problems/0401-binary-watch/solution_daily_20260217.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math/bits" +) + +// readBinaryWatch enumerates all valid times (h:0-11, m:0-59) and collects +// those whose total set bit count in h and m equals turnedOn. +func readBinaryWatch(turnedOn int) []string { + var result []string + for h := 0; h < 12; h++ { + for m := 0; m < 60; m++ { + if bits.OnesCount(uint(h))+bits.OnesCount(uint(m)) == turnedOn { + result = append(result, fmt.Sprintf("%d:%02d", h, m)) + } + } + } + return result +} diff --git a/problems/0401-binary-watch/solution_daily_20260217_test.go b/problems/0401-binary-watch/solution_daily_20260217_test.go new file mode 100644 index 0000000..15687a6 --- /dev/null +++ b/problems/0401-binary-watch/solution_daily_20260217_test.go @@ -0,0 +1,76 @@ +package main + +import ( + "sort" + "testing" +) + +func TestReadBinaryWatch(t *testing.T) { + tests := []struct { + name string + turnedOn int + expected []string + }{ + { + name: "example 1: turnedOn=1", + turnedOn: 1, + expected: []string{"0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", "8:00"}, + }, + { + name: "example 2: turnedOn=9 impossible", + turnedOn: 9, + expected: []string{}, + }, + { + name: "edge case: turnedOn=0 only midnight", + turnedOn: 0, + expected: []string{"0:00"}, + }, + { + name: "edge case: turnedOn=10 impossible", + turnedOn: 10, + expected: []string{}, + }, + { + name: "turnedOn=2 multiple combinations", + turnedOn: 2, + expected: []string{ + "0:03", "0:05", "0:06", "0:09", "0:10", "0:12", "0:17", "0:18", "0:20", "0:24", + "0:33", "0:34", "0:36", "0:40", "0:48", + "1:01", "1:02", "1:04", "1:08", "1:16", "1:32", + "2:01", "2:02", "2:04", "2:08", "2:16", "2:32", + "3:00", + "4:01", "4:02", "4:04", "4:08", "4:16", "4:32", + "5:00", "6:00", + "8:01", "8:02", "8:04", "8:08", "8:16", "8:32", + "9:00", "10:00", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := readBinaryWatch(tt.turnedOn) + + // Sort both slices for comparison since order doesn't matter + sort.Strings(result) + sort.Strings(tt.expected) + + if len(tt.expected) == 0 && len(result) == 0 { + return // both empty, pass + } + + if len(result) != len(tt.expected) { + t.Errorf("got %d results, want %d\ngot: %v\nwant: %v", len(result), len(tt.expected), result, tt.expected) + return + } + + for i := range result { + if result[i] != tt.expected[i] { + t.Errorf("mismatch at index %d: got %q, want %q\nfull got: %v\nfull want: %v", i, result[i], tt.expected[i], result, tt.expected) + return + } + } + }) + } +}