zoukankan      html  css  js  c++  java
  • 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
    class Solution {
        public int largestOverlap(int[][] A, int[][] B) {
            int ans = 0;
            for (int row = -A.length; row < A.length; row++) {
                for (int col = -A[0].length; col < A[0].length; col++) {
                    ans = Math.max(ans, overlap(A, B, row, col));
                }
            }
            return ans;
        }
        public int overlap(int[][] A, int[][] B, int rowOffset, int colOffset) {
            int ans = 0;
            for (int row = 0; row < A.length; row++) {
                for (int col = 0; col < A[0].length; col++) {
                    if ((row+rowOffset < 0 || row+rowOffset >= A.length) || (col + colOffset < 0 || col + colOffset >= A.length)) {
                        continue;
                    }
                    if(A[row][col] == 1 && B[row + rowOffset][col+colOffset] == 1) {
                        ans++;
                    }
                }
            }
            return ans;
        }
    }

    对比所有的点位,看哪个位置重叠的最多

  • 相关阅读:
    花生壳 manjaro 安装
    manjaro+apache+django+mod_wsgi 安装
    arch linux或 Manjaro下安装 微信 wechat deepin-wine-wechat
    BBU+RRU基本介绍
    黑马python01——基础
    NNLearning阶段性总结01
    【信息论】——第二讲
    10.09——今日文章收集
    pygame安装【在pycharm的IDE project下】
    Git笔记——01
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13642317.html
Copyright © 2011-2022 走看看