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的使用也没有想象中的那么容易。

  • 相关阅读:
    初探 Linux
    操作系统简介
    1208. 尽可能使字符串相等
    643. 子数组最大平均数 I
    480. 滑动窗口中位数
    Bisect in Python
    HTTP 和 HTTPS 的区别
    URI和URL的区别
    HTTP 1.0和HTTP 1.1的主要区别是什么?
    MySQL游标的使用笔记大全
  • 原文地址:https://www.cnblogs.com/jinjin-2018/p/9034105.html
Copyright © 2011-2022 走看看