835. 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

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

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10669690.html

时间: 2024-07-30 17:31:09

835. Image Overlap的相关文章

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),

Leetcode 835. Image Overlap.md

题目 链接:https://leetcode.com/problems/image-overlap/ Level: Medium Discription: 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 cho

CoreOS 835.12.0 稳定版安装

导读 CoreOS是一个基于Docker的轻量级容器化Linux发行版,为Docker而生,CoreOS作为Docker生态圈中的重要一员,日益得到各大云服务商的重视,发展风头正劲. CoreOS宣称最小化的定制版linux系统:  Linux内核 Linux运行所需存在两个ROOT分区,一个被用作启动分区,一个被用作更新分区更新分区在更新完成后,自动重新启动系统,当前机器不需要从负载集群中移除,为了保证其它应用程序不被打断,会通过Linux cgroup限制更新过程中的磁盘.网络等IO使用.

解决:配置虚拟主机,重启apache,[warn] _default_ VirtualHost overlap on port 80, the first has precedence

http://blog.csdn.net/kaizhu_qin/article/details/17506293 很多第一次配置apache的虚拟主机的时候,以为配置第一个虚拟主机完成以后,以后就不会出现什么问题了.在配置第一个虚拟主机的时候,重启apache的时候,都可能会遇到下面的问题:[warn] _default_ VirtualHost overlap on port 80, the first has precedence是因为第一个虚拟主机配置已经占用了80端口,所以将会沿用第一虚

text label overlap probelm

常见的解决方法有三种: 1. 使用wordcloud #install.packages(c("wordcloud","tm"),repos="http://cran.r-project.org") library(wordcloud) library(tm) wordcloud("May our children and our children's children to a thousand generations, contin

[warn] _default_ VirtualHost overlap on port 80, the first has precedence

解决[warn] _default_ VirtualHost overlap on port 80, the first has precedence问题 在apache2的httpd.conf里新增加了1个VirtualHost,域名是www.ligh.com,此时,服务器总共2个VirtualHost ,apachectl restart的时候却出现了下面的警告提示: 大概意思就是说后面新增加的这个VirtualHost 由于端口被占用,不能生效,沿用第一个虚拟主机的配置.  www.2ct

12 Overlap Graphs

Problem A graph whose nodes have all been labeled can be represented by an adjacency list, in which each row of the list contains the two node labels corresponding to a unique edge. A directed graph (or digraph) is a graph containing directed edges,

socket-重叠模型(overlap)

重叠模型的基本设计原理便是让应用程序使用一个重叠的数据结构,一次投递一个或多个Winsock I/O请求.针对那些提交的请求,在它们完成之后,应用程序可为它们提供服务.该模型适用于除Windows CE之外的各种Windows平台.模型的总体设计以Win32重叠I/O机制为基础.那个机制可以通过ReadFile和WriteFile两个函数,针对设备执行I/O操作. 关键是理解“重叠”两个字,就是你把发送的数据交给系统,然后自己做别的事情,在你干自己的事情时,系统同时也正在完成你交给他的任务,两者

洛谷P2202 [USACO13JAN]方块重叠Square Overlap

P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000) square fenced-in pastures on his farm, each of size exactly K x K (1 <= K <= 1,000,000). Pasture i is centered at point (x_i, y_i) with integer coo