-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path835.java
More file actions
59 lines (54 loc) · 1.96 KB
/
835.java
File metadata and controls
59 lines (54 loc) · 1.96 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
49
50
51
52
53
54
55
56
57
58
59
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int size = A.length;
int maxOverlap = 0;
// shift A first
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int overlap = calcOverlap(A, B, i, j);
maxOverlap = Math.max(overlap, maxOverlap);
}
}
// then shift B
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int overlap = calcOverlap(B, A, i, j);
maxOverlap = Math.max(overlap, maxOverlap);
}
}
return maxOverlap;
}
private int calcOverlap(int[][] A, int[][] B, int shiftX, int shiftY) {
int len = A.length;
int overlap = 0;
for (int i = shiftY; i < len; i++) {
for (int j = shiftX; j < len; j++) {
overlap += A[i - shiftY][j - shiftX] * B[i][j];
}
}
return overlap;
}
}
__________________________________________________________________________________________________
sample 37388 kb submission
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int N = A.length;
int[][] count = new int[2*N+1][2*N+1];
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (A[i][j] == 1)
for (int i2 = 0; i2 < N; ++i2)
for (int j2 = 0; j2 < N; ++j2)
if (B[i2][j2] == 1)
count[i-i2 +N][j-j2 +N] += 1;
int ans = 0;
for (int[] row: count)
for (int v: row)
ans = Math.max(ans, v);
return ans;
}
}
__________________________________________________________________________________________________