You are given two images, img1 and img2, represented as binary, square matrices of size n x n. A binary matrix has only 0s and 1s as values.
We translate one image however we choose by sliding all the 1 bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the overlap by counting the number of positions that have a 1 in both images.
Note also that a translation does not include any kind of rotation. Any 1 bits that are translated outside of the matrix borders are erased.
Return the largest possible overlap.
Example 1:
Input: img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]] Output: 3 Explanation: We translate img1 to right by 1 unit and down by 1 unit.The number of positions that have a 1 in both images is 3 (shown in red).
![]()
Example 2:
Input: img1 = [[1]], img2 = [[1]] Output: 1
Example 3:
Input: img1 = [[0]], img2 = [[0]] Output: 0
Constraints:
n == img1.length == img1[i].lengthn == img2.length == img2[i].length1 <= n <= 30img1[i][j] is either 0 or 1.img2[i][j] is either 0 or 1.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Think of two partially transparent pictures stacked on top of each other. The brute force method involves sliding one picture across the other in every possible way, and counting how many parts line up perfectly. It's like physically moving one image and checking every single position.
Here's how the algorithm would work step-by-step:
def image_overlap_brute_force(first_image, second_image):
max_overlap_count = 0
first_image_size = len(first_image)
second_image_size = len(second_image)
for row_offset in range(-second_image_size + 1, first_image_size):
for column_offset in range(-second_image_size + 1, first_image_size):
overlap_count = 0
# Iterate through cells of the second image
for second_image_row in range(second_image_size):
for second_image_column in range(second_image_size):
first_image_row = second_image_row + row_offset
first_image_column = second_image_column + column_offset
# Ensure current cell is within bounds of the first image
if (0 <= first_image_row < first_image_size and
0 <= first_image_column < first_image_size):
# Count matching ones
if (first_image[first_image_row][first_image_column] == 1 and
second_image[second_image_row][second_image_column] == 1):
overlap_count += 1
# Update the overall max value
max_overlap_count = max(max_overlap_count, overlap_count)
# Return the maximum overlap count
return max_overlap_countThe fastest way to solve this image overlap problem involves counting how many times a '1' in one image lands on top of a '1' in the other image for every possible way you can shift one image relative to the other. Instead of shifting the entire image, we only need to consider shifts where at least one '1' overlaps.
Here's how the algorithm would work step-by-step:
def image_overlap(image_one, image_two):
image_one_ones = []
image_two_ones = []
for row_index in range(len(image_one)):
for col_index in range(len(image_one[0])):
if image_one[row_index][col_index] == 1:
image_one_ones.append((row_index, col_index))
if image_two[row_index][col_index] == 1:
image_two_ones.append((row_index, col_index))
shift_counts = {}
# Iterate through all pairs of ones to calculate shifts.
for image_one_row, image_one_col in image_one_ones:
for image_two_row, image_two_col in image_two_ones:
row_shift = image_one_row - image_two_row
col_shift = image_one_col - image_two_col
if (row_shift, col_shift) in shift_counts:
shift_counts[(row_shift, col_shift)] += 1
else:
shift_counts[(row_shift, col_shift)] = 1
max_overlap = 0
# Find the shift with the maximum overlap count.
for shift in shift_counts:
max_overlap = max(max_overlap, shift_counts[shift])
return max_overlap| Case | How to Handle |
|---|---|
| Image A or B is null or empty | Return 0, as no overlap is possible with an empty image. |
| Image A or B has dimensions of 1x1 | Iterate through all possible shifts in the smaller image to count overlaps. |
| Image A and B are identical | The maximum possible overlap (size of image A) should be returned by shifting image B so it is on top of A (0, 0 shift). |
| Images A and B contain all zeros | The overlap count should return the size of Image A (or B), as every position will be overlapping 0s. |
| Images A and B are of vastly different sizes (e.g., one is 10x10, the other is 100x100) | The iteration should consider only the valid shift ranges, limiting the overall complexity. |
| Images A and B contain only 1s | The overlap count should return the minimum dimension size minus shifts when image B moves. |
| Integer overflow when calculating overlap sums (if not using boolean representation) | Use data types with sufficient range (e.g., long) to store the overlap sums, or use boolean addition (if data is binary). |
| No overlap exists between the images (all positions are misaligned) | The algorithm must return 0 in the case where no shift leads to any overlapping 1s. |