Image Overlap

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
 1 class Solution {
2 public:
3 int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
4 int n = A.size();
5 int res = 0;
6 for(int i = -n+1; i < n; i++){
7 for(int j =-n+1; j < n; j++){
8 int count = 0;
9 for(int k = max(i,0); k < min(n+i,n); k++){
10 for(int l = max(j,0); l < min(n+j,n);l++){
11 if(A[k-i][l-j]==1&&B[k][l]==1){
12 count++;
13 }
14 }
15 }
16 res = max(res,count);
17 }
18 }
19 return res;
20 }
21 };

暴力模拟,O(n4),模拟过程的边界以及max的使用也没有想象中的那么容易。

835. Image Overlap —— weekly contest 84的更多相关文章

  1. 836. Rectangle Overlap ——weekly contest 85

    Rectangle Overlap A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coor ...

  2. 833. Find And Replace in String —— weekly contest 84

    Find And Replace in String To some string S, we will perform some replacement operations that replac ...

  3. 834. Sum of Distances in Tree —— weekly contest 84

    Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges a ...

  4. 832. Flipping an Image —— weekly contest 84

    Flipping an Image Given a binary matrix A, we want to flip the image horizontally, then invert it, a ...

  5. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  6. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

随机推荐

  1. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  2. 【奇淫巧技】XSS绕过技巧

    XSS记录 1.首先是弹窗函数: alert(1) prompt(1) confirm(1)eval() 2.然后是字符的编码和浏览器的解析机制: 要讲编码绕过,首先我们要理解浏览器的解析过程,浏览器 ...

  3. OpenCV 中Scalar

    参考来源: 1.https://blog.csdn.net/Eroslol/article/details/52525541 2.https://www.cnblogs.com/hustdc/p/68 ...

  4. 用Python爬取B站、腾讯视频、爱奇艺和芒果TV视频弹幕!

    众所周知,弹幕,即在网络上观看视频时弹出的评论性字幕.不知道大家看视频的时候会不会点开弹幕,于我而言,弹幕是视频内容的良好补充,是一个组织良好的评论序列.通过分析弹幕,我们可以快速洞察广大观众对于视频 ...

  5. c++ 西安交通大学 mooc 第十三周基础练习&第十三周编程作业

    做题记录 风影影,景色明明,淡淡云雾中,小鸟轻灵. c++的文件操作已经好玩起来了,不过掌握好控制结构显得更为重要了. 我这也不做啥题目分析了,直接就题干-代码. 总结--留着自己看 1. 流是指从一 ...

  6. Docker学习:(一)初识Docker

    Docker(容器虚拟化技术)要点(秒级启动) Docker的WWH公式学习 What[是什么]. Why[为什么要用它]. How[怎么用] 1.Docker简介 (1)问题:为什么会有docker ...

  7. 多测师讲解python函数 _open_高级讲师肖sir

    open()函数 #open() 函数用于打开一个文件,创建一个 file 对象 #Python open() 函数用于打开一个文件,并返回文件对象, # 在对文件进行处理过程都需要使用到这个函数,如 ...

  8. day67:Vue:es6基本语法&vue.js基本使用&vue指令系统

    目录 Vue前戏:es6的基本语法 1.es6中的let特点 1.1.局部作用域 1.2.不存在变量提升 1.3.不能重复声明 1.4.let声明的全局变量不从属于window对象,var声明的全局变 ...

  9. 在shell中截取心仪的字符串

    file=/dir1/dir2/dir3/my.file.txt ${file#*/} 去掉左边 ${file##*/} 去掉左边最后一个 ${file%/*} 去掉右边 ${file%%/*} 去掉 ...

  10. centos8平台用ffprobe获取视频文件信息(ffmpeg4.2.2)

    一,ffprobe的作用 ffprobe是强大的视频分析工具, 用于从多媒体流中获取相关信息或查看文件格式信息, 并以可读的方式打印 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...