-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_watch.go
More file actions
69 lines (62 loc) · 1.27 KB
/
Copy pathbinary_watch.go
File metadata and controls
69 lines (62 loc) · 1.27 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
package main
import "fmt"
func readBinaryWatch(turnedOn int) []string {
hours := map[int][]int{
0: {0},
}
minutes := map[int][]int{
0: {0},
}
nums := []int{1, 2, 4, 8, 16, 32}
queue := make([]int, 0)
var calculate func(idx, count, target, sum int)
calculate = func(idx, count, target, sum int) {
sum += nums[idx]
count += 1
if count == target {
queue = append(queue, sum)
return
}
if idx == len(nums) {
return
}
for i := idx + 1; i < len(nums); i++ {
calculate(i, count, target, sum)
}
}
for i := 0; i < 6; i++ {
for j := 1; j <= turnedOn; j++ {
calculate(i, 0, j, 0)
for k := 0; k < len(queue); k++ {
res := queue[k]
if res > 0 {
if res <= 11 {
if _, ok := hours[j]; !ok {
hours[j] = []int{}
}
hours[j] = append(hours[j], res)
}
if res <= 59 {
if _, ok := minutes[j]; !ok {
minutes[j] = []int{}
}
minutes[j] = append(minutes[j], res)
}
}
}
queue = queue[:0]
}
}
ans := make([]string, 0)
for i := 0; i <= turnedOn; i++ {
j := turnedOn - i
if hours[i] != nil && minutes[j] != nil {
for _, hour := range hours[i] {
for _, minute := range minutes[j] {
ans = append(ans, fmt.Sprintf("%d:%02d", hour, minute))
}
}
}
}
return ans
}