-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathoutput-contest-matches.go
More file actions
48 lines (39 loc) · 1.01 KB
/
output-contest-matches.go
File metadata and controls
48 lines (39 loc) · 1.01 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
package main
import (
"fmt"
"strconv"
"strings"
)
func findContestMatch(n int) string {
array := make([]string, n)
for pos, _ := range make([]int, n) {
array[pos] = strconv.Itoa(pos + 1)
}
for power := 1; power < n; power *= 2 {
tmp_arr := make([]string, n+n/power/2)
for slice, _ := range make([]int, n/power/2) {
start := slice * power
copy(tmp_arr[start*2:start*2+power], array[start:start+power])
copy(tmp_arr[start*2+power:start*2+power*2], array[n-start-power:n-start])
}
array = tmp_arr
}
var dfs func(array []string, output []string, pow int) []string
pos := 0
dfs = func(array []string, output []string, pow int) []string {
if pow == 1 {
output = append(output, array[pos])
pos += 1
} else {
output = append(output, "(")
output = dfs(array, output, pow/2)
output = append(output, ",")
output = dfs(array, output, pow/2)
output = append(output, ")")
}
return output
}
output := []string{}
output = dfs(array, output, n)
return strings.Join(output, "")
}