zoukankan      html  css  js  c++  java
  • 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. 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的使用也没有想象中的那么容易。

  • 相关阅读:
    一分钟应对勒索病毒WannaCry
    你不知道网络安全有多严峻
    MongoDB 文章目录
    SQL Server 文章目录
    MySQL 文章目录
    领域驱动(DD)目录
    Oracle基本教程
    系统架构研究目录
    设计原则目录
    开源项目学习历程
  • 原文地址:https://www.cnblogs.com/jinjin-2018/p/9034105.html
Copyright © 2011-2022 走看看