Image Overlap LeetCode solution in Python
1# Runtime: 593 ms (Top 76.00%) | Memory: 14.7 MB (Top 44.00%)2class Solution:3def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int:4n = len(img1)5list1, list2 = [], []6res = 07for r in range(n):8for c in range(n):9if img1[r][c]:10list1.append((r, c))11if img2[r][c]:12list2.append((r, c))1314shiftDict = defaultdict(int)15for x1, y1 in list1:16for x2, y2 in list2:17dx, dy = x2 - x1, y2 - y118shiftDict[(dx, dy)] += 11920return max(shiftDict.values()) if shiftDict else 0