Skip to content

Commit b609d12

Browse files
committed
added problem 835
1 parent 97c5550 commit b609d12

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

Diff for: problems/problem0835/image_overlap.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package problem0835
2+
3+
/*
4+
You are given two images, img1 and img2, represented as binary, square matrices of size n x n. A binary matrix has only 0s and 1s as values.
5+
We translate one image however we choose by sliding all the 1 bits left, right, up, and/or down any number of units.
6+
We then place it on top of the other image. We can then calculate the overlap by counting the number of positions that have a 1 in both images.
7+
Note also that a translation does not include any kind of rotation. Any 1 bits that are translated outside of the matrix borders are erased.
8+
Return the largest possible overlap.
9+
*/
10+
11+
func largestOverlap(img1 [][]int, img2 [][]int) int {
12+
var res, cur int
13+
var n int = len(img1)
14+
var ni, nj int
15+
for oi := -(n - 1); oi <= (n-1)*2; oi++ {
16+
for oj := -(n - 1); oj <= (n-1)*2; oj++ {
17+
cur = 0
18+
for i := 0; i < n; i++ {
19+
ni = i + oi
20+
if ni >= n || ni < 0 {
21+
continue
22+
}
23+
for j := 0; j < n; j++ {
24+
nj = j + oj
25+
if nj >= n || nj < 0 {
26+
continue
27+
}
28+
if img1[ni][nj] == 1 && img2[i][j] == 1 {
29+
cur++
30+
}
31+
}
32+
}
33+
if cur > res {
34+
res = cur
35+
}
36+
}
37+
}
38+
return res
39+
}
40+
41+
func largestOverlapOpt(img1 [][]int, img2 [][]int) int {
42+
var res int
43+
var n = len(img1)
44+
var LA, LB []int = make([]int, 0, n), make([]int, 0, n)
45+
var count = map[int]int{}
46+
for i := 0; i < n*n; i++ {
47+
if img1[i/n][i%n] == 1 {
48+
LA = append(LA, i/n*100+i%n)
49+
}
50+
if img2[i/n][i%n] == 1 {
51+
LB = append(LB, i/n*100+i%n)
52+
}
53+
}
54+
for _, i := range LA {
55+
for _, j := range LB {
56+
count[i-j]++
57+
}
58+
}
59+
for _, c := range count {
60+
if c > res {
61+
res = c
62+
}
63+
}
64+
return res
65+
}
66+
67+
/*
68+
int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
69+
vector<int> LA, LB;
70+
int N = A.size();
71+
unordered_map<int, int> count;
72+
for (int i = 0; i < N * N; ++i)
73+
if (A[i / N][i % N] == 1)
74+
LA.push_back(i / N * 100 + i % N);
75+
for (int i = 0; i < N * N; ++i)
76+
if (B[i / N][i % N] == 1)
77+
LB.push_back(i / N * 100 + i % N);
78+
for (int i : LA) for (int j : LB) count[i - j]++;
79+
int res = 0;
80+
for (auto it : count) res = max(res, it.second);
81+
return res;
82+
*/

Diff for: problems/problem0835/image_overlap_test.go

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package problem0835
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input [][]int
12+
Target [][]int
13+
Expected int
14+
}
15+
16+
var Results = []Result{
17+
{[][]int{{1, 1, 0}, {0, 1, 0}, {0, 1, 0}}, [][]int{{0, 0, 0}, {0, 1, 1}, {0, 0, 1}}, 3},
18+
{[][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, [][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 0},
19+
{[][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, [][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 9},
20+
{[][]int{{1, 0}, {0, 1}}, [][]int{{0, 1}, {1, 0}}, 1},
21+
{[][]int{{0, 0}, {0, 0}}, [][]int{{0, 0}, {0, 0}}, 0},
22+
{[][]int{{1, 1}, {1, 1}}, [][]int{{1, 1}, {1, 1}}, 4},
23+
{
24+
[][]int{
25+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
26+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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, 1, 1, 1, 1},
83+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
84+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
85+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
86+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
87+
}, 900},
88+
}
89+
90+
func TestImageOverlap(t *testing.T) {
91+
assert := assert.New(t)
92+
93+
for _, res := range Results {
94+
want := res.Expected
95+
got := largestOverlap(res.Input, res.Target)
96+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
97+
}
98+
}
99+
100+
func BenchmarkImageOverlap(b *testing.B) {
101+
for i := 0; i < b.N; i++ {
102+
for _, res := range Results {
103+
largestOverlap(res.Input, res.Target)
104+
}
105+
}
106+
}
107+
108+
func TestImageOverlapOpt(t *testing.T) {
109+
assert := assert.New(t)
110+
111+
for _, res := range Results {
112+
want := res.Expected
113+
got := largestOverlapOpt(res.Input, res.Target)
114+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
115+
}
116+
}
117+
118+
func BenchmarkImageOverlapOpt(b *testing.B) {
119+
for i := 0; i < b.N; i++ {
120+
for _, res := range Results {
121+
largestOverlapOpt(res.Input, res.Target)
122+
}
123+
}
124+
}

Diff for: readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Each problem is in it's own directory, with test files. There are helper package
162162
| 0814 | [Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning) | [My Solution](./problems/problem0814) ||
163163
| 0820 | [Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words) | [My Solution](./problems/problem0820) ||
164164
| 0823 | [Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors) | [My Solution](./problems/problem0823) ||
165+
| 0835 | [Image Overlap](https://leetcode.com/problems/image-overlap) | [My Solution](./problems/problem0835) ||
165166
| 0838 | [Push Dominoes](https://leetcode.com/problems/push-dominoes) | [My Solution](./problems/problem0838) ||
166167
| 0858 | [Mirror Reflection](https://leetcode.com/problems/mirror-reflection) | [My Solution](./problems/problem0858) ||
167168
| 0867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix) | [My Solution](./problems/problem0867) ||

0 commit comments

Comments
 (0)