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

Runtime: 103 ms, faster than 42.68% of Java online submissions for Image Overlap.

注意,求两个点的向量差值,唯一表示要把坐标分开来,

这一题中,坐标小于30,所以La,Lb中add了i/N * 100 + i% N, i/N 和 i % N分别是横纵坐标。

最后的表示是一个4位数,xxxx。前两位是横坐标,后两位是纵坐标。

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> dist = new HashMap<>();
for(int i=; i<N*N; i++) if(A[i/N][i%N] == ) La.add(i/N* + i%N);
for(int j=; j<N*N; j++) if(B[j/N][j%N] == ) Lb.add(j/N* + j%N);
for(int i : La){
for(int j : Lb){
dist.put(i - j, dist.getOrDefault(i-j, ) + );
}
}
int ret = ;
for(int i : dist.values()) ret = Math.max(i, ret);
return ret;
}
}

LC 835. Image Overlap的更多相关文章

  1. 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. 836. Rectangle Overlap 矩形重叠

    [抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...

随机推荐

  1. 人工智能_4_k近邻_贝叶斯_模型评估

    机器学习常用算法 k近邻算法 求出未知点 与周围最近的 k个点的距离 查看这k个点中大多数是哪一类 根号((x已知-x未知)^2+(y已知-y未知)^2) 即平面间2点距离公式 收异常点影响较大,因此 ...

  2. css 之calc无效踩坑

    踩坑: 1. height:calc(100vh-60);  无效 2.height:calc(100vh-60px); 无效 3.height:calc(100vh - 60px);  终于起效 总 ...

  3. springboot Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    报错如下: 在请求目标中发现无效字符.有效字符在RFC 7230和RFC 3986中定义. 原因是Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证. 就是 ...

  4. Kubernetes的初始化容器initContainers

    initContainers是一种专用的容器,在应用程序容器启动之前运行,可以包括一些应用程序镜像中不存在的实用工具和安装脚本,可以完成应用的必要数据初始化等工作.总的来说就是在正式的容器启动之前做一 ...

  5. 关于使用jquery评论插件...

    .今天做项目,使用了一个评论插件 调用出来没事, 可是添加的时候报错 Uncaught TypeError: $(...).find(...).live is not a function 这个错误 ...

  6. python文件操作:字符编码与文件处理

    一.字符编码 二.文件处理 一.字符编码 储备知识点: 1. 计算机系统分为三层: 应用程序 操作系统 计算机硬件 2. 运行python程序的三个步骤 1. 先启动python解释器 2. 再将py ...

  7. vue-element-admin后台的安装

    # 克隆项目 git clone https://github.com/PanJiaChen/vue-element-admin.git # 进入项目目录 cd vue-element-admin # ...

  8. [Abp vNext微服务实践] - vue-element-admin登录二

    简介: Vue Element Admin是基于vue.element ui开发的后台管理ui,abp vNext是abp新一代微服务框架.本篇将会介绍如何改造Vue Element Admin权限验 ...

  9. Python Flask学习笔记(1)

    1.搭建虚拟环境 a. 安装 virtualenv : pip3 install virtualenv b. 建立虚拟环境 : 任意目录下建立一个空文件(我的是 Py_WorkSpace) ,在该文件 ...

  10. 小辣椒之lombok-1.18.8.jar的使用

    1.下载lombok-1.18.8.jar包 http://central.maven.org/maven2/org/projectlombok/lombok/1.18.8/lombok-1.18.8 ...