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. 织梦后台系统设置在PHP5.4环境中不能保存中文参数的解决方法

    在没用PHP5.4的环境做Dede后台的时候,织梦58一直没有遇到这个问题,昨天上传一个新的模版到空间去测试发现后台的系统基本参数设置中所有的中文内容都无法保存,关于这个问题,其实以前也听说过,知识一 ...

  2. 翻译应用将在Win8.1系统中取消下载安装

    自Windows8.Windows Phone 7.1和Windows Phone 8受到影响之后,微软又正式宣布停止对翻译应用提供支持服务.Microsoft Translator这款应用将从Win ...

  3. V.24 V.35 ISDN E1 POS这些常见的广域网接口

    转:http://blog.sina.com.cn/s/blog_bc1c78600101l2ss.html 广域网(Wide Area Network)是一种跨地区的数据通讯网络,通常是一个局域网到 ...

  4. 在线p图网址

    在线P图网址 如果你是简易的P图,不用那么麻烦的去下载安装Photoshop,可以使用以下网址在线编辑 https://www.uupoop.com/ps/?hmsr=ps_menu

  5. linux-2.6.38 input子系统(简析)

    一.输入子系统简介 引入输入子系统这种机制可以对不同的输入设备进行管理.各种输入设备如:鼠标.键盘.触摸屏等有一套相同的处理机制,输入子系统将其共性提取出来, 对于驱动开发人员只用实现其差异即可,实现 ...

  6. Redis使用总结-基础篇

    年底的时候开始尝试在重构的项目中使用redis,现在项目稳定运行也有一段时间了,这里做一下阶段性总结. 一.简介 首先,redis是什么意思呢,官方文档的FAQ里给出了答案:It means REmo ...

  7. idea中iml文件的问题

    idea中iml文件的问题 iml文件是idea组织工程的文件, 里面记录了各种记录模块, 文件夹以及依赖的信息, 显示如下: <?xml version="1.0" enc ...

  8. 【Java 关键字this 的使用】还阔以调用重载的构造方法

    笔记: /** this 关键字的使用除了调用方法和变量外, * 还可以用来显示 调用当前类的重载的指定的构造方法! * 同时也应该必须放到该方法内部的首行! */ 测试: import java.l ...

  9. iframe通信相关:父操作子,子操作父,兄弟通信

    这里写window和document是认为代表了BOM和DOM(个人理解不一定对) 拿到了window就能操作全局变量和函数 拿到了document就能获取dom,操作节点 父操作子 window:选 ...

  10. linux基础_使用指令3

    时间日期类 1.date指令 功能:显示当前日期 语法: date:显示当前时间 date +%Y:显示当前年份 date +%m:显示当前月份 date +%d:显示当前是哪一天 date &quo ...