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. lua table vs closure

    最近在重构自己写的框架中的定时器模块,需要把回调函数保存起来,大概如下: function timer_mgr:save_timer( this,callback ) return { this = ...

  2. redis——搭建

    https://blog.csdn.net/sinat_29699167/article/details/79699200 Django使用Redis进行缓存详细最全流程 https://blog.c ...

  3. Redis入门(一)——安装

    1.下载地址,选择对应版本 https://github.com/dmajkic/redis/downloads 2.下载解压后,进入到文件夹,地址栏cmd,redis-server.exe redi ...

  4. UVa1078 Steam Roller——拆点+最短路

    题目链接 思路 把每个点拆成\(5\)个点\(id(x,y),id(x,y)+n,id(x,y)+2*n,id(x,y)+3*n,id(x,y)+4*n\),分别表示到这个点时的方向为上,右,下,左和 ...

  5. 微信小程序实现连接蓝牙设备跑步APP

    背景 微信小程序兴起,有变成超级APP的趋势,通过微信提供的小程序api,可以通过微信调用到手机原生的支持. 目标 通过微信小程序实现来实现跑步类App的功能. 需求分析 跑步类App需要的两个核心的 ...

  6. vue 后退不刷新,前进刷新 keep-alive

    最近在开发中遇到了这样的一个问题: A.B.C三个页面,有如下这样的场景: (1)从页面A离开进入B或C的时候,缓存A页面的数据,并且返回到A后,能保持A页面的跳转前职位 (2)离开B进入C的时候,缓 ...

  7. Acwing-287-积蓄程度(树上DP, 换根)

    链接: https://www.acwing.com/problem/content/289/ 题意: 有一个树形的水系,由 N-1 条河道和 N 个交叉点组成. 我们可以把交叉点看作树中的节点,编号 ...

  8. vue 01 练习

    1.有 红.黄.蓝 三个按钮,以及一个200x200矩形框box,点击不同的按钮,box的颜色会被切换为指定的颜色 <!DOCTYPE html> <html lang=" ...

  9. APPLICATION FAILED TO START 报错

    错误一 原因@Service 忘记加了

  10. word文档的图片怎么保存到ueditor上

    word图片转存,是指UEditor为了解决用户从word中复制了一篇图文混排的文章粘贴到编辑器之后,word文章中的图片数据无法显示在编辑器中,也无法提交到服务器上的问题而开发的一个操作简便的图片转 ...