835. Image Overlap Medium

@problem@discussion
#Array#Matrix



1/**
2 * [835] Image Overlap
3 *
4 * 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.
5 * 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.
6 * 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.
7 * Return the largest possible overlap.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/overlap1.jpg" style="width: 450px; height: 231px;" />
11 * Input: img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]
12 * Output: 3
13 * Explanation: We translate img1 to right by 1 unit and down by 1 unit.
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/overlap_step1.jpg" style="width: 450px; height: 105px;" />
15 * The number of positions that have a 1 in both images is 3 (shown in red).
16 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/overlap_step2.jpg" style="width: 450px; height: 231px;" />
17 * 
18 * Example 2:
19 * 
20 * Input: img1 = [[1]], img2 = [[1]]
21 * Output: 1
22 * 
23 * Example 3:
24 * 
25 * Input: img1 = [[0]], img2 = [[0]]
26 * Output: 0
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	n == img1.length == img1[i].length
32 * 	n == img2.length == img2[i].length
33 * 	1 <= n <= 30
34 * 	img1[i][j] is either 0 or 1.
35 * 	img2[i][j] is either 0 or 1.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/image-overlap/
41// discuss: https://leetcode.com/problems/image-overlap/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn largest_overlap(img1: Vec<Vec<i32>>, img2: Vec<Vec<i32>>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_835() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.