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. ADF backing Bean中常用的代码

    // 刷新iterator bindings.refreshControl(); iterBind.executeQuery(); iterBind.refresh(DCIteratorBinding ...

  2. springmvc中@RequestMapping的使用

    通过RequestMapping注解可以定义不同的处理器映射规则. 1.1 URL路径映射 @RequestMapping(value="/item")或@RequestMappi ...

  3. Python打杂之路

    1.任务要落到纸上好记性不如烂笔头,再好的记性也不如写到纸上明确无误,写到纸上就不用担心会漏掉哪项工作.平时,我们总是在忙着一项工作的同时还惦记着下一项工作,把工作都记下后,我们就可以专注于一项工作, ...

  4. mysql rc模式时binlog_format=row的解释【转】

    总体来说:在 tx_isolation= READ-COMMITTED .binlog_format =statement 的情况下,mysql 没有gap 锁,这样binlog 记录的数据修改的顺序 ...

  5. linux下安装php php-fpm(转载)

    centos安装php php-fpm 1.下载php源码包http://www.php.net/downloads.php2 .安装phptar -xvf php-5.5.13.tar.bz2cd ...

  6. apache的80端口被占用

    1.netstart -ano | findstr "80":查看80端口是否被占用,并找出对应的pid 2.关掉pid对应的进程

  7. [SoapUI] Tips and Tricks(提示和技巧)

    https://www.soapui.org/scripting-properties/tips-tricks.html

  8. jQuery Datepicker 插件遇到问题

    Datepicker ver 1.7.3  浏览更多 常用设置 //禁用今天之前时间 $(".datePicker").datepicker('option', { minDate ...

  9. 594. Longest Harmonious Subsequence

    方法一:用一个map来辅助,代码简单,思路清晰 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }( ...

  10. METAGENOMIC SEQUENCING ANALYSIS WORKFLOW

    Metagenomics is defined as the study of the metagenome, which is total genomic DNA from environmenta ...