Skip to main content

835. Image Overlap

https://leetcode.com/problems/image-overlap/

Python

class Solution:
def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int:
n = len(img1)

def count_overlay(start_row, start_col):
up_left, up_right, down_left, down_right = 0, 0, 0, 0

for row_ref, row_move in enumerate(range(start_row, n)):
for col_ref, col_move in enumerate(range(start_col, n)):
if img1[row_move][col_move] and img2[row_ref][col_ref]:
up_left += 1

if img1[row_move][col_ref] and img2[row_ref][col_move]:
up_right += 1

if img1[row_ref][col_ref] and img2[row_move][col_move]:
down_left += 1

if img1[row_ref][col_move] and img2[row_move][col_ref]:
down_right += 1

return max(up_left, up_right, down_left, down_right)

ans = 0
for r in range(n):
for c in range(n):
ans = max(ans, count_overlay(r, c))
return ans