Leetcode 835. Image Overlap.md

题目

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

Level: Medium

Discription:
Two images A and B are given, 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: A = [[1,1,0],
            [0,1,0],
            [0,1,0]]
       B = [[0,0,0],
            [0,1,1],
            [0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.

Note:

  • 1 <= A.length = A[0].length = B.length = B[0].length <= 30
  • 0 <= A[i][j], B[i][j] <= 1

代码

class Solution {
public:
    int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
        map<vector<int>,int> count;
        int max=0;
        for(int i=0; i<A[0].size()*A[0].size(); i++ )
        {
            int AX=i/A[0].size();
            int AY=i%A[0].size();
            if(A[AX][AY]==0)
                continue;
            for(int j=0;j<B[0].size()*B[0].size(); j++)
            {
                int BX=j/B[0].size();
                int BY=j%B[0].size();
                if(B[BX][BY]==1)
                {
                    vector<int> temp;
                    temp.push_back(BX-AX);
                    temp.push_back(BY-AY);
                    if(count.find(temp)!=count.end())
                    {
                        count[temp]++;
                    }
                    else
                        count[temp]=1;
                    max = count[temp]>max ? count[temp] : max;
                }
            }
        }
        return max;
    }
};

思考

  • 算法时间复杂度为O(\(n^4\)),空间复杂度为O(\(n^2\) ),n是矩阵的边长。
  • 思路是统计转移方向向量的个数,一个二元数组即可记录所有可能的转移。因为使用map和vector,并且全部遍历了一遍,最终的时间复杂度和空间复杂度都很高。
  • 通过测试发现这题的数据有问题。看到一个解法直接去数转移后的重叠1的个数,但是只考虑了向右向下和向左向上的转移,但是也通过了。而这样的数据就通不过了:
    [[0,1,1],[0,1,1],[0,0,0]]
    [[0,0,0],[1,1,0],[1,1,0]]
  • 由上面的另一种题解说明,通过模拟转移后的矩阵,计算重叠的1的个数的思路是可行的,运行时间和空间都减少了10多倍。但是仍然没有想到复杂度更低的算法,效率更高的算法可能用到FFT等信号处理的知识,暂时就不研究了。

原文地址:https://www.cnblogs.com/zuotongbin/p/10498673.html

时间: 2024-07-30 17:10:39

Leetcode 835. Image Overlap.md的相关文章

835. Image Overlap

Two images A and B are given, 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 o

835. Image Overlap —— weekly contest 84

Image Overlap Two images A and B are given, 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),

leetcode 836. Rectangle Overlap

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.  To b

[leetcode] 90. 子集 II.md

90. 子集 II 78. 子集题的扩展,其中的元素可能会出现重复了 我们仍沿用78题的代码,稍作改动即可: 此时需要对nums先排个序,方便我们后面跳过选取相同的子集. 跳过选取相同的子集.当选取完第i个数时,如果后面的数,和当前数相同,就跳过,不必对其进行递归了. class Solution { private void dfs(int n, int k, int last, int[] nums, List<Integer> cur, List<List<Integer&g

Leetcode:835. 图像重叠

直接找出所有1的位置,然后对两个矩阵的所有这些位置进行求差.然后统计这些差出现最多的次数是多少. 两个坐标的差是什么含义?就是把其中一个坐标移动到另一个坐标需要移动的向量.因此,在遍历过程中,我们找出了A中所有值为1的坐标移动到B中所有值为1的坐标需要移动的向量.那么,在这些向量中出现次数最多的向量就是我们要求的整个矩阵应该移动的向量.这个向量出现的次数,就是我们向该向量方向移动了之后,能重叠的1的个数. 寒神的做法的优点:第一,注意到了题目给的A和B是大小相等的正方形!第二,遍历正方形的方式使

[LeetCode] Image Overlap 图像重叠

Two images A and B are given, 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 o

[LeetCode] Rectangle Overlap 矩形重叠

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.  To b

【一天一道LeetCode】#165. Compare Version Numbers.md

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: https://leetcode.com/problems/compare-version-numbers/ Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 <

【一天一道LeetCode】#292. Nim Game.md

一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to