835. Image Overlap —— weekly contest 84
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 <= A.length = A[0].length = B.length = B[0].length <= 300 <= 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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
随机推荐
- 请编写sql多语句表值函数统,计指定年份中每本书的销售总额
create table 图书表( 书号 varchar(50), 书名 varchar(50), 单价 int ) create table 销售表( 书号 varchar(50), 销售时间 da ...
- Matlab 中 imshow 函数
转自: https://blog.csdn.net/xiaochou87/article/details/43488829 matlab中显示图像的语句是: ...
- 50个你必须了解的Kubernetes面试问题
Kubernetes一直是当今业界的流行语,也是最好的编排工具.它吸引了许多想要提升自己职业生涯的经验丰富的专业人士.HuaWei,Pokemon,Box,eBay,Ing,Yahoo Japan,S ...
- 加快ASP。NET Core WEB API应用程序。第2部分
下载source from GitHub 使用各种方法来增加ASP.NET Core WEB API应用程序的生产力 介绍 第1部分.创建测试RESTful WEB API应用程序第2部分.增加了AS ...
- 【总结】Oracle数据库 查看表空间和增加表空间
一.Oracle查看 表空间 的名称及其大小 查看 表空间的名称及其大小的SQL语句: select t1.tablespace_name,round(sum(bytes/(1024*1024)),0 ...
- ansible-playbook调试
1. ansible-playbook 1)ansible-playbook的语法检测 1 [root@test-1 bin]# ansible-playbook --syntax-check ng ...
- git冲突的表现
<<<<<<< HEAD b789 ======= b45678910 >>>>>>> 6853e5ff961e68 ...
- Redis 的完整安装过程
Windos 版本安装 Redis 官方并不支持 Window 版本,但是微软公司在 Github 上维护了一个 Windows 版本的 Redis 项目,供 Windows 用户下载使用. 下载地址 ...
- Mysql架构与内部模块-第一章
Mysql作为大多数中小型企业的首选数据库,也可能是众多同僚接触的第一个数据库,其热门程度不言而喻,一些相对基础的知识本系列不做赘述,主要简述Mysql相关的进阶知识. 本章将由浅入深的讲解从连接My ...
- 基于SpringAop的鉴权功能
什么是 AOP 首先我们先了解一下什么是AOP,AOP(Aspect Orient Programming),直译过来就是面向切面编程.AOP是一种编程思想,是面向对象编程(OOP)的一种补充.面向对 ...