leetcode 835. Image Overlap(图像重叠)

You are given two images img1 and img2 both of size n x n, represented as binary, square matrices of the same size. (A binary matrix has only 0s and 1s as values.)

We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image. After, the overlap of this translation is the number of positions that have a 1 in both images.

(Note also that a translation does not include any kind of rotation.)

What is 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 slide img1 to right by 1 unit and down by 1 unit.

求两个image的overlap,overlap是指重叠到一块的时候相同位置都是1的个数。
img1和img2都可上下左右移动,但不能翻转。

思路:
普通的img shift一般是往右移一格一格移,移到边界的时候下移一行再右移。暂且称为右下移动。
但是这里img可以4个方向shift,左上,左下,右上,右下
在这里插入图片描述
粗暴的方法就是每个方向写一个循环,每个循环里找最大的overlap,但是可以思考一下,
img1和img2叠在一起的时候,img1向上移动就相当于img2向下移动,同理左右移动。所以img1循环右下的时候,(1) img1和img2调换一下位置(img2右下),就变成了img1左上。这样就解决了2个方向。

同时,(2) img1循环右下的时候,比如走到了(0,1)位置,第一个overlap的位置是看img1[0][1]和img2[0][0]是否相等,把两个img的列换一下,img1[0][0]和img2[0][1]就相当于img2右移,也就是img1左移。那么循环右下的时候同时解决了左下和右下两个方向。

(2) (1)结合,img1循环右下,调换列,解决了左下和右下两个方向,再调换img1和img2的位置,就在一个循环里解决了四个方向。在4个方向里找最大的overlap。
但是时间复杂度是不变的,都是O(N4)

class Solution {
    int row = 0;
    int col = 0;
    public int largestOverlap(int[][] img1, int[][] img2) {
        row = img1.length;
        col = img1[0].length;
        int result = 0;
        for(int i = 0; i < row; i++) { //row shift
            for(int j = 0; j < col; j++) {  //col shift
                result = Math.max(result, count(i, j, img1, img2));
                result = Math.max(result, count(i, j, img2, img1));
            }
        }
        return result;
    }
    
    int count(int sr, int sc, int[][] img1, int[][] img2) {
        int overlap1 = 0;
        int overlap2 = 0;
        for(int i = sr; i < row; i++) {
            for(int j = sc; j < col; j++) {
                overlap1 += img1[i-sr][j-sc] & img2[i][j]; //right shift
                overlap2 += img1[i-sr][j] & img2[i][j-sc]; //left shift
            }
        }
        return Math.max(overlap1, overlap2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值