作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/image-overlap/description/

题目描述

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 <= 30
  • 0 <= A[i][j], B[i][j] <= 1

题目大意

两个形状相同的正方形矩阵,求用什么体位把它两个重叠在一起,然后重叠部分中1的个数最多。

解题方法

思路挺好玩的,直接找出所有1的位置,然后对两个矩阵的所有这些位置进行求差。然后统计这些差出现最多的次数是多少。

两个坐标的差是什么含义?就是把其中一个坐标移动到另一个坐标需要移动的向量。因此,在遍历过程中,我们找出了A中所有值为1的坐标移动到B中所有值为1的坐标需要移动的向量。那么,在这些向量中出现次数最多的向量就是我们要求的整个矩阵应该移动的向量。这个向量出现的次数,就是我们向该向量方向移动了之后,能重叠的1的个数。

结尾的or [0]挺有意思,防止了d.values()是空。

Python代码如下:

class Solution:
def largestOverlap(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: int
"""
N = len(A)
LA = [(xi, yi) for xi in range(N) for yi in range(N) if A[xi][yi]]
LB = [(xi, yi) for xi in range(N) for yi in range(N) if B[xi][yi]]
d = collections.Counter([(x1 - x2, y1 - y2) for (x1, y1) in LA for (x2, y2) in LB])
return max(d.values() or [0])

二刷的时候使用C++,首先是上面同样的做法,没有优化的代码如下:

class Solution {
public:
int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
map<pair<int, int>, int> move;
vector<pair<int, int>> A1s, B1s;
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < A[0].size(); ++j) {
if (A[i][j])
A1s.push_back({i, j});
}
}
for (int i = 0; i < B.size(); ++i) {
for (int j = 0; j < B[0].size(); ++j) {
if (B[i][j])
B1s.push_back({i, j});
}
}
int res = 0;
for (auto a : A1s) {
for (auto b : B1s) {
pair<int, int> dir = make_pair(b.first - a.first, b.second - a.second);
move[dir]++;
res = max(res, move[dir]);
}
}
return res;
}
};

上面这个代码确实啰嗦了,看了寒神的答案,感觉自愧不如啊!

寒神的做法的优点:第一,注意到了题目给的A和B是大小相等的正方形!第二,遍历正方形的方式使用的是i[0,N*N)区间里,然后[i/N][i%N]这个求位置方法,可以把两重循环简写成一重(但是时间复杂没有变化)。第三,使用了数字表示向量,即把一个向量的行数*100+列数,比如第13行第19列,可以用一个数字表示1319。寒神告诉我们,这个100的选择是因为太小的话不能有效区分,应该最小是2N。

class Solution {
public:
int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
const int N = A.size();
unordered_map<int, int> move;
vector<int> A1s, B1s;
for (int i = 0; i < N * N; ++i) {
if (A[i / N][i % N])
A1s.push_back((i / N) * 100 + i % N);
if (B[i / N][i % N])
B1s.push_back((i / N) * 100 + i % N);
}
int res = 0;
for (auto a : A1s) {
for (auto b : B1s) {
move[b - a]++;
res = max(res, move[b - a]);
}
}
return res;
}
};

参考资料:

https://blog.csdn.net/zjucor/article/details/80298134
https://leetcode.com/problems/image-overlap/discuss/150504/Python-Easy-Logic
https://leetcode.com/problems/image-overlap/discuss/130623/C%2B%2BJavaPython-Straight-Forward

日期

2018 年 9 月 10 日 —— 教师节快乐!
2018 年 12 月 28 日 —— 元旦假期到了

【LeetCode】835. Image Overlap 解题报告(Python & C++)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. nginx——网站显示问题

    一般来说修改3个位置,一个是nginx.h.另一个是ngx_http_header_filter_module.c.还有一个ngx_http_special_response.c. 提示:一般修改都是 ...

  2. python—模拟生成双色球号

    双色球规则:"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中不重复选择:蓝色球号码从1--16中选择. # -*- coding:UTF-8 - ...

  3. n组字母和最大

    字母A-J,用0-9对应字母使得n组数据和最大,输入字符串前面保证非0 如输入组数据: 2 ABC BCA 输出: 1875 思路:其实就是求和,对应字符乘以相应的量级,按系数排序 如上MAX(101 ...

  4. JForum论坛安装以及部署

    转载链接:https://blog.csdn.net/jhyfugug/article/details/79467369 首先安装JForum之前,先准备好安装环境Windows7+JDK+Tomca ...

  5. Hive(二)【数据类型、类型转换】

    目录 一.基本数据类型 案例实操 二.集合数据类型 案例实操 Map类型 三.类型转换 1.隐式类型转换 2.显示(强制)类型转换 一.基本数据类型 HIVE MySQL JAVA 长度 例子 TIN ...

  6. 零基础学习java------day1------计算机基础以及java的一些简单了解

    一. java的简单了解 Java是一门面向对象编程语言,不仅吸收了C++的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态 ...

  7. C++最小内积

    Description 向量是几何中的一个重要概念. 考虑两个向量 v1=(x1,x2,...,xn)和v2=(y1,y2,...,yn),向量的内积定义为 x1y1+x2y2+...+xnyn 例如 ...

  8. android转换透明度

    比方说 70% 白色透明度. 就用255*0.7=185.5  在把185.5转换成16进制就是B2 你只需要写#B2FFFFFF 如果是黑色就换成6个0就可以了.前2位是控制透明度的.

  9. JavaBean的命名规则

    JavaBean的命名规则Sun 推荐的命名规范1 ,类名要首字母大写,后面的单词首字母大写2 ,方法名的第一个单词小写,后面的单词首字母大写3 ,变量名的第一个单词小写,后面的单词首字母大写为了使 ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py #!/usr/b ...