-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathImageOverlap.java
21 lines (21 loc) · 1005 Bytes
/
ImageOverlap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int rows = A.length, cols = A[0].length;
List<int[]> la = new ArrayList<>(), lb = new ArrayList<>(); // two lists to save pixel coordinates
for (int r = 0; r<rows; r++)
for (int c = 0; c<cols; c++){
if (A[r][c] == 1) la.add(new int[]{r,c}); // save the pixel coordinates
if (B[r][c] == 1) lb.add(new int[]{r,c});
}
Map<String, Integer> map = new HashMap<>(); // map to map the vector (from a pixel in A to a pixel in B) to its count
for (int[] pa : la)
for (int[] pb : lb) {
String s = (pa[0] - pb[0]) + " " + (pa[1]-pb[1]); // get the vector from a pixel in A to a pixel in B
map.put(s, map.getOrDefault(s, 0) + 1); // count the number of same vectors
}
int max = 0;
for (int count : map.values())
max = Math.max(max, count);
return max;
}
}