-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0401-binary-watch.go
More file actions
85 lines (69 loc) · 2.37 KB
/
0401-binary-watch.go
File metadata and controls
85 lines (69 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package backtracking
import (
"fmt"
"math/bits"
)
// https://leetcode.com/problems/binary-watch/
// Approach 1: Brute Force - Try All Possible Times
// Iterate through all possible hours (0-11) and minutes (0-59).
// For each combination, count the number of bits set.
// If it matches turnedOn, add to result.
// Time: O(12 * 60) = O(720) = O(1) - constant time
// Space: O(1) - not counting output
// Approach 2: Backtracking
// Use backtracking to generate all combinations of positions
// where LEDs can be turned on.
// Time: O(C(10, turnedOn)) - combinatorial
// Space: O(turnedOn) - recursion depth
// Approach 3: Bit Manipulation (Optimal for this problem)
// Count set bits in hour (0-11) and minute (0-59).
// If total set bits equals turnedOn, add to result.
// This is actually simpler than backtracking for this specific problem.
// Time: O(12 * 60) = O(1)
// Space: O(1)
func readBinaryWatch(turnedOn int) []string {
result := []string{}
// Try all possible hours (0-11) and minutes (0-59)
for hour := range 12 {
for minute := range 60 {
// Count total bits set in both hour and minute
if bits.OnesCount(uint(hour))+bits.OnesCount(uint(minute)) == turnedOn {
// Format: h:mm (hour without leading zero, minute with leading zero)
result = append(result, fmt.Sprintf("%d:%02d", hour, minute))
}
}
}
return result
}
// // Alternative: Backtracking approach
// func readBinaryWatch(turnedOn int) []string {
// result := []string{}
// // positions[i] represents the value at position i
// // First 4 positions are hours: 8, 4, 2, 1
// // Last 6 positions are minutes: 32, 16, 8, 4, 2, 1
// positions := []int{8, 4, 2, 1, 32, 16, 8, 4, 2, 1}
// var backtrack func(index, count, hours, minutes int)
// backtrack = func(index, count, hours, minutes int) {
// // Pruning: if already invalid, stop
// if hours >= 12 || minutes >= 60 {
// return
// }
// // Base case: used all turnedOn LEDs
// if count == turnedOn {
// result = append(result, fmt.Sprintf("%d:%02d", hours, minutes))
// return
// }
// // Try turning on each remaining LED
// for i := index; i < len(positions); i++ {
// if i < 4 {
// // Hour LED
// backtrack(i+1, count+1, hours+positions[i], minutes)
// } else {
// // Minute LED
// backtrack(i+1, count+1, hours, minutes+positions[i])
// }
// }
// }
// backtrack(0, 0, 0, 0)
// return result
// }