-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path835-Image_Overlap.go
More file actions
49 lines (39 loc) · 855 Bytes
/
Copy path835-Image_Overlap.go
File metadata and controls
49 lines (39 loc) · 855 Bytes
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
func largestOverlap(img1 [][]int, img2 [][]int) int {
n := len(img1)
ones1 := getOnesPositions(img1, n)
ones2 := getOnesPositions(img2, n)
if len(ones1) == 0 || len(ones2) == 0 {
return 0
}
translationCount := make(map[string]int)
for _, pos1 := range ones1 {
for _, pos2 := range ones2 {
dx := pos2[1] - pos1[1]
dy := pos2[0] - pos1[0]
key := fmt.Sprintf("%d,%d", dx, dy)
translationCount[key]++
}
}
maxOverlap := 0
for _, count := range translationCount {
maxOverlap = max(maxOverlap, count)
}
return maxOverlap
}
func getOnesPositions(img [][]int, n int) [][2]int {
positions := [][2]int{}
for r := 0; r < n; r++ {
for c := 0; c < n; c++ {
if img[r][c] == 1 {
positions = append(positions, [2]int{r, c})
}
}
}
return positions
}
func max(a, b int) int {
if a > b {
return a
}
return b
}