Problem
Two images A
and B
are given, represented as binary, square matrices of the same size. (A binary matrix has only 0s and 1s as values.)
We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image. After, the overlap of this translation is the number of positions that have a 1 in both images.
(Note also that a translation does not include any kind of rotation.)
What is the largest possible overlap?
Example 1:
Input: A = [[1,1,0],
[0,1,0],
[0,1,0]]
B = [[0,0,0],
[0,1,1],
[0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.
Notes:
1 <= A.length = A[0].length = B.length = B[0].length <= 30
0 <= A[i][j], B[i][j] <= 1
Solution 1
/**
* @param {number[][]} A
* @param {number[][]} B
* @return {number}
*/
var largestOverlap = function(A, B) {
var len = A.length;
var res = 0;
var tmp = 0;
for (var i = 1 - len; i < len; i++) {
for (var j = 1 - len; j < len; j++) {
tmp = 0;
for (var k = 0; k < len; k++) {
for (var m = 0; m < len; m++) {
if (B[k][m] === 1 && A[k + i] && A[k + i][m + j] === 1) tmp++;
}
}
res = Math.max(res, tmp);
}
}
return res;
};
Explain:
nope.
Complexity:
- Time complexity : O(n^4).
- Space complexity : O(1).
Solution 2
/**
* @param {number[][]} A
* @param {number[][]} B
* @return {number}
*/
var largestOverlap = function(A, B) {
var len = A.length;
var arrA = [];
var arrB = [];
var count = {};
var key = 0;
var max = 0;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len; j++) {
if (A[i][j] === 1) arrA.push(i * 100 + j);
if (B[i][j] === 1) arrB.push(i * 100 + j);
}
}
for (var m = 0; m < arrA.length; m++) {
for (var n = 0; n < arrB.length; n++) {
key = arrA[m] - arrB[n];
if (!count[key]) count[key] = 0;
count[key]++;
}
}
for (key in count) {
max = Math.max(max, count[key]);
}
return max;
};
Explain:
找出 A
, B
中所有的 1
。比如 A[1][1] = 1
与 B[2][3] = 1
,这两个 1
要对应上的话,A
要下移一位,再右移两位,分配一个独立的 key
代表这个移动,即 1 * 100 + 2
。按照移动的类型,统计最大值。
Complexity:
- Time complexity : O(n^2).
- Space complexity : O(n).