-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
81 lines (70 loc) · 1.38 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"io"
"math"
"math/big"
"strconv"
)
func fs(input io.Reader, groups int) (string, error) {
scanner := bufio.NewScanner(input)
var packages []int
sum := 0
for scanner.Scan() {
s := scanner.Text()
i, err := strconv.Atoi(s)
if err != nil {
return "", err
}
packages = append(packages, i)
sum += i
}
solutions := all(0, packages, 0, sum/groups, nil)
min := math.MaxInt
for _, solution := range solutions {
min = getMin(min, len(solution))
}
var mingQE *big.Int
for _, solution := range solutions {
if len(solution) == min {
v := qe(solution)
if mingQE == nil {
mingQE = v
} else if v.Cmp(mingQE) == -1 {
mingQE = v
}
}
}
return mingQE.String(), nil
}
func qe(solution []int) *big.Int {
res := big.NewInt(1)
res.SetString("1", 10)
for _, i := range solution {
n := new(big.Int)
n.Mul(res, big.NewInt(int64(i)))
res = n
}
return res
}
func getMin(a, b int) int {
if a < b {
return a
}
return b
}
func all(idx int, packages []int, w, target int, current []int) [][]int {
if w == target {
return [][]int{current}
}
if idx == len(packages) || w > target {
return nil
}
weight := packages[idx]
var res [][]int
// With
res = append(res, all(idx+1, packages, w+weight, target, append(current, weight))...)
// Without
res = append(res, all(idx+1, packages, w, target, current)...)
return res
}