-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
44 lines (42 loc) · 960 Bytes
/
main.js
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
/**
* @param {number[][]} A
* @param {number[][]} B
* @return {number}
*/
var largestOverlap = function (A, B) {
function cnt (A, B) {
let ans = 0
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (A[i][j] === B[i][j] && A[i][j] === 1) {
ans += 1
}
}
}
return ans
}
function move (A, x, y) {
const next = Array.from(A, (_, i) => A[i].slice().fill(0))
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const nx = x + i
const ny = y + j
if (nx >= rows || ny >= cols) {
continue
}
next[nx][ny] = A[i][j]
}
}
return next
}
const rows = A.length
const cols = A[0].length
let ans = 0
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
ans = Math.max(ans, cnt(move(A, i, j), B))
ans = Math.max(ans, cnt(move(B, i, j), A))
}
}
return ans
}