835. Image Overlap
Two images
AandBare 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 <= A.length = A[0].length = B.length = B[0].length <= 300 <= 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的更多相关文章
- LC 835. Image Overlap
Two images A and B are given, represented as binary, square matrices of the same size. (A binary ma ...
- 【leetcode】835. Image Overlap
题目如下: 解题思路:抛开移动的过程只看移动完成的结果,记图片左上角为顶点,正方形边长为board,要使得两个图片要有重叠,那么一定其中一张图片的顶点和另外一张图片的某一个点重合.假设图片A的顶点A( ...
- 835. Image Overlap —— weekly contest 84
Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...
- 【LeetCode】835. Image Overlap 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- The Daligner Overlap Library
/************************************************************************************\ * * * Copyrig ...
- [LeetCode] Image Overlap 图像重叠
Two images A and B are given, represented as binary, square matrices of the same size. (A binary ma ...
- CoreOS 835.12.0 稳定版安装
导读 CoreOS是一个基于Docker的轻量级容器化Linux发行版,为Docker而生,CoreOS作为Docker生态圈中的重要一员,日益得到各大云服务商的重视,发展风头正劲. CoreOS宣称 ...
随机推荐
- golang协程进行同步方法
1.使用chanel func main() { done := make(chan bool) ticker := time.NewTicker(time.Millisecond * 1000) g ...
- laravel的函数asset()、url()
1.asset():用于引入静态文件,如 css/JavaScript/images,文件必须存放在public文件目录下 src="{{ asset('home') }}/images/t ...
- 向一个文件流写入一个数据块---fwrite
函数原型:int fwrite(const void *buffer,size_t size,size_t count,FILE *stream); 参数说明:buffer:用于写入到文件的数据地址. ...
- 【Log】logback的配置和使用(一)
logback介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access ...
- 2018.08.10 atcoder No Need(线性dp)
传送门 输入一个序列an" role="presentation" style="position: relative;">anan,输入k&q ...
- 2018.07.09 洛谷P2365 任务安排(线性dp)
P2365 任务安排 题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间 ...
- hdu-1026(bfs+优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...
- linux安装mysql~~~mysql5.6.12
Linux安装mysql服务器 准备: MySQL-client-5.6.12-1.rhel5.i386.rpm MySQL-server-5.6.12-1.rhel5.i386.rpm 首先检查环境 ...
- UVaLive 3487 Duopoly (最小割)
题意:有两个公司A和B在申请一些资源,现在给出两个公司所申请的内容,内容包括价钱和申请的资源 ,现在你做为官方,你只能拒绝一个申请或者接受一个申请,同一个资源不能两个公司都拥有,且申请的资源不能只给部 ...
- Vue组件通信父传方法给子组件调用
// 父组件中将 :meth='changeCom1' 传入入子组件 , 子组件运行 meth(i) 方法 并给他传参数 ,在父组件可以获取这个参数,并做相应的操作 // 父组件 <temp ...