-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathImageOverlap.java
45 lines (40 loc) · 1.58 KB
/
ImageOverlap.java
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
class Solution {
private int shiftRightDown(int[][] A, int[][] B, int xShift, int yShift, int N) {
//i represents row i.e towards y axis
//j represents column i.e towards x axis
int count = 0;
for (int i = yShift; i < N; i++) {
for (int j = xShift; j < N; j++) {
if (A[i][j] == 1 && B[i - yShift][j - xShift] == 1)
count++;
}
}
return count;
}
private int shiftRightUp(int[][] A, int[][] B, int xShift, int yShift, int N) {
int count = 0;
for (int i = yShift; i < N; i++) {
for (int j = 0; j < N - xShift; j++) {
if (A[i][j] == 1 && B[i - yShift][j + xShift] == 1)
count++;
}
}
return count;
}
public int largestOverlap(int[][] A, int[][] B) {
int maxCount = 0;
int N = A.length;
//xShift & yShift will be equivalent to the starting row, col of the sliding matrix over fixed matrix
//i.e (1,0) the overlap started between
//1st Matrix moving, 2nd fixed
for (int yShift = 0; yShift < N; yShift++) {
for (int xShift = 0; xShift < N; xShift++) {
maxCount = Math.max(maxCount, shiftRightDown(A, B, xShift, yShift, N));
maxCount = Math.max(maxCount, shiftRightDown(B, A, xShift, yShift, N));
maxCount = Math.max(maxCount, shiftRightUp(A, B, xShift, yShift, N));
maxCount = Math.max(maxCount, shiftRightUp(B, A, xShift, yShift, N));
}
}
return maxCount;
}
}