-
Notifications
You must be signed in to change notification settings - Fork 1
/
17.go
69 lines (57 loc) · 1.26 KB
/
17.go
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 main() {
target := 150
containers := []int{11, 30, 47, 31, 32, 36, 3, 1, 5, 3, 32, 36, 15, 11, 46, 26, 28, 1, 19, 3}
var ids []int
for i := range containers {
ids = append(ids, i)
}
queue := [][]int{}
solutions := [][]int{}
// bootstrap queue
for _, id := range ids {
queue = append(queue, []int{id})
}
for {
if len(queue) == 0 {
break
}
var item []int
item, queue = queue[0], queue[1:]
used := 0
for _, id := range item {
used += containers[id]
}
if used == target {
solutions = append(solutions, item)
continue
}
for _, nextContainer := range ids {
if nextContainer < item[len(item)-1] {
if containers[nextContainer]+used <= target {
/* avoid references */
itemCopy := make([]int, len(item))
copy(itemCopy, item)
queue = append(queue, append(itemCopy, nextContainer))
}
}
}
}
fmt.Println(len(solutions))
minLength := len(containers)
for _, solution := range solutions {
if len(solution) < minLength {
minLength = len(solution)
}
}
minSolutions := 0
for _, solution := range solutions {
if len(solution) == minLength {
minSolutions++
}
}
fmt.Println(minSolutions)
}