diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/image_overlap.rs | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/image_overlap.rs')
-rw-r--r-- | other/image_overlap.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/other/image_overlap.rs b/other/image_overlap.rs new file mode 100644 index 0000000..fa6fbd6 --- /dev/null +++ b/other/image_overlap.rs @@ -0,0 +1,35 @@ +impl Solution { + pub fn largest_overlap(img1: Vec<Vec<i32>>, img2: Vec<Vec<i32>>) -> i32 { + let mut ans = 0; + for i in 0..img1[0].len() { + for j in 00..img1.len() { + ans = ans.max(Solution::shift_and_compare(&img1, &img2, j, i)); + } + } + ans + } + + fn shift_and_compare(img1: &[Vec<i32>],img2: &[Vec<i32>], x_shift: usize, y_shift: usize) -> i32 { + let (mut counter, cols, rows) = ((0,0), img1.len(), img1[0].len()); + for i in 0..cols { + for j in 0..rows { + if img1[i][j] == 1 { + let ii = i + y_shift; + let jj = j + x_shift; + if ii < rows && jj < cols && img2[ii][jj] == 1 { + counter.0 += 1; + } + } + if img2[i][j] == 1 { + let ii = i + y_shift; + let jj = j + x_shift; + if ii < rows && jj < cols && img2[ii][jj] == 1 { + counter.1 += 1; + } + } + } + } + counter.0.max(counter.1) + } +} + |