zoukankan      html  css  js  c++  java
  • LC 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

    Runtime: 103 ms, faster than 42.68% of Java online submissions for Image Overlap.

    注意,求两个点的向量差值,唯一表示要把坐标分开来,

    这一题中,坐标小于30,所以La,Lb中add了i/N * 100 + i% N, i/N 和 i % N分别是横纵坐标。

    最后的表示是一个4位数,xxxx。前两位是横坐标,后两位是纵坐标。

    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> dist = 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 j=0; j<N*N; j++) if(B[j/N][j%N] == 1) Lb.add(j/N*100 + j%N);
        for(int i : La){
          for(int j : Lb){
            dist.put(i - j, dist.getOrDefault(i-j, 0) + 1);
          }
        }
        int ret = 0;
        for(int i : dist.values()) ret = Math.max(i, ret);
        return ret;
      }
    }
  • 相关阅读:
    window.clipboardData(转载)
    动态添加样式(转载)
    IE6 IE7 FF的CSS Hack总结(转载)
    [轉貼] linux解壓 tar 命令
    [轉]用 snprintf / asprintf 取代不安全的 sprintf
    寫一個函數計算當參數為 n(n很大) 時的值 12+34+56+7……+n
    [轉]vi 與 vim 的指令整理
    MySQL和php採用UTF8的方法
    [轉]printf 引數說明
    [C] warning: ISO C90 forbids mixed declarations and code
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10256336.html
Copyright © 2011-2022 走看看