-
Notifications
You must be signed in to change notification settings - Fork 5
/
day-159.cpp
62 lines (48 loc) · 1.64 KB
/
day-159.cpp
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
60
61
62
/*
Image Overlap
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
*/
// Sliding window by (N - 1) in every direction (top,down,right,left)
// Beats 93.98 % of cpp submissions.
class Solution {
public:
int shiftCount(vector<vector<int>>& A, vector<vector<int>>& B) {
int N = A.size();
int count = 0;
for (int idx = 0; idx < N; idx++) {
for (int jidx = 0; jidx < N; jidx++) {
int temp = 0;
for (int x = jidx; x < N; x++) {
for (int y = idx; y < N; y++) {
if (A[x][y] == 1 && B[x - jidx][y - idx] == 1)
temp += 1;
}
}
count = max(count, temp);
}
}
return count;
}
int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
return max(shiftCount(A, B), shiftCount(B, A));
}
};