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.

Notes:

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

Approach #1: Array. [Java]

class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int N = A.length;
List<Integer> LA = new ArrayList<>();
List<Integer> LB = new ArrayList<>();
HashMap<Integer, Integer> count = new HashMap<>();
for (int i = 0; i < N * N; ++i) if (A[i/N][i%N] == 1)
LA.add(i / N * 100 + i % N);
for (int i = 0; i < N * N; ++i) if (B[i/N][i%N] == 1)
LB.add(i / N * 100 + i % N);
for (int i : LA) for (int j : LB)
count.put(i-j, count.getOrDefault(i-j, 0) + 1);
int res = 0;
for (int i : count.values()) res = Math.max(res, i);
return res;
}
}

  

Analysis:

Assume index in A and B is [0, N*N-1]

Loop on A, if value == 1, save a coordinates i / N * 100 + i % N to LA.

Loop on B, if value == 1, save a coordinates i / N * 100 + i % N to LB.

Loop on combination (i, j) of LA and LB, increase count[i-j] by 1.

If we slid to make A[i] orverlap B[j], we can get 1 point.

Loop on count and return max values.

Time Complexity:

O(N^2) for preparing, and O(AB) for loop.

O(AB + N^2)

Reference:

https://leetcode.com/problems/image-overlap/discuss/130623/C%2B%2BJavaPython-Straight-Forward

835. Image Overlap的更多相关文章

  1. LC 835. Image Overlap

    Two images A and B are given, represented as binary, square matrices of the same size.  (A binary ma ...

  2. 【leetcode】835. Image Overlap

    题目如下: 解题思路:抛开移动的过程只看移动完成的结果,记图片左上角为顶点,正方形边长为board,要使得两个图片要有重叠,那么一定其中一张图片的顶点和另外一张图片的某一个点重合.假设图片A的顶点A( ...

  3. 835. Image Overlap —— weekly contest 84

    Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...

  4. 【LeetCode】835. Image Overlap 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. The Daligner Overlap Library

    /************************************************************************************\ * * * Copyrig ...

  8. [LeetCode] Image Overlap 图像重叠

    Two images A and B are given, represented as binary, square matrices of the same size.  (A binary ma ...

  9. CoreOS 835.12.0 稳定版安装

    导读 CoreOS是一个基于Docker的轻量级容器化Linux发行版,为Docker而生,CoreOS作为Docker生态圈中的重要一员,日益得到各大云服务商的重视,发展风头正劲. CoreOS宣称 ...

随机推荐

  1. DB2 SQL1477N问题

    ERROR [55019] [IBM][DB2/NT] SQL1477N  For table "DB_YHJX.YHJX_FHDKFHZ" an object "521 ...

  2. PAT 1015 德才论 (25)(代码+思路)

    1015 德才论 (25)(25 分)提问 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子, ...

  3. IE8以下支持css3 border-radius渲染方法

    这两天在做个集团网站,web前端妹子技术水平不咋样,写个web和wap 真够费劲的,对之前流行的H5和css3 响应式看来不太会用,扔给我一个半成品~~·非说各种canvas和border-radiu ...

  4. 记录如何用abd,用电脑输出手机操作信号

    0.http://www.wmzhe.com/soft-39913.html 去这里下载最新版的adb.旧版本很多不好使.一定最新的. 1.用豌豆荚装好驱动 2.开启usb调试.具体方法可以百度到 3 ...

  5. 构造函数constructor 与析构函数destructor(五)

    我们知道当调用默认拷贝构造函数时,一个对象对另一个对象初始化时,这时的赋值时逐成员赋值.这就是浅拷贝,当成员变量有指针时,浅拷贝就会在析构函数那里出现问题.例如下面的例子: //test.h #ifn ...

  6. python的介绍和及基本的使用

    一 什么是计算机 1 计算机就是由一堆硬件组成的一个机器. 2 硬件的分类: CPU:犹如人类的大脑,运行着需要运行的程序. 内存:将 CPU要运行的内容从硬盘中读取出来,然后CPU在内存里拿内容,只 ...

  7. DEVEXPRESS 破解方法

    Devexpress 是.net的一个非常好用的插件.能够轻松的帮你实现一个非常炫的UI,无论是C#的Winform还是ASP.NET的网站. 鄙人这两天在用DEVEXPRESS的过程中发现在网上并未 ...

  8. PHP数据库抽象层--PDO(PHP Data Object) [一]

    1.简介:(PDO数据库访问抽象层,统一各种 数据库的访问接口 ) PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库 ...

  9. cuDNN

    https://developer.nvidia.com/developer-program https://developer.nvidia.com/cudnn cuda和cuDNN的关系 http ...

  10. (匹配)Dolls --HDU --4160

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4160 代码: #include<stdio.h> #include<string.h& ...