zoukankan      html  css  js  c++  java
  • 835. Image Overlap

    问题:

    给定两个大小相同,由0和1构成的N*N矩阵。求将其中一个矩阵进行水平垂直平移,最终两个矩阵重叠(1)的最大面积为多少。

    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 <= A.length = A[0].length = B.length = B[0].length <= 30
    0 <= A[i][j], B[i][j] <= 1
    

      

    解法:

    先分别存取两个矩阵为1的坐标到LA和LB中。

    再遍历两个矩阵任意两点,求其偏移vector到map的vectorcout中,记录相同偏移的计数。

    最终求的最大计数的偏移。

    代码参考:

     1 class Solution {
     2 public:
     3     int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
     4         vector<vector<int>> LA, LB;//coordinate 坐标
     5         int N = A.size();
     6         int res=0;
     7         for(int i=0; i<N; i++){
     8             for(int j=0; j<N; j++){
     9                 if(A[i][j]==1) LA.push_back({i,j});
    10                 if(B[i][j]==1) LB.push_back({i,j});
    11             }
    12         }
    13         unordered_map<string, int> vectorcout;
    14         for(vector<int> la:LA){
    15             for(vector<int> lb:LB){
    16                 string vector=to_string(la[0]-lb[0])+"-"+to_string(la[1]-lb[1]);
    17                 vectorcout[vector]++;
    18             }
    19         }
    20         for(auto k:vectorcout){
    21             res=max(res, k.second);
    22         }
    23         return res;
    24     }
    25 };

    ♻️ 优化:

    降低存储花费。

    将坐标<i, j>化为整数数值 i*N+j

    但是本问题中,涉及到任意两点的偏移,会有正反两个方向,因此如果取N作为倍率,将会重现多种偏移对应一个值的情况。

    如:(N=30)409 = 13 * 30 + 19 = 14 * 30 - 11.
    409 可由<13, 19>或者<14,-11>两种偏移得到。

    我们需要一个值对应一个偏移的一一对应,

    因此取得倍率,至少应为2*N

    这里我们取倍率为100

    代码参考:

     1 class Solution {
     2 public:
     3     int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
     4         vector<int> LA, LB;//coordinate 坐标
     5         int N = A.size();
     6         int res=0;
     7         for(int i=0; i<N*N; i++){
     8                 if(A[i/N][i%N]==1) LA.push_back(i/N*100+i%N);
     9                 if(B[i/N][i%N]==1) LB.push_back(i/N*100+i%N);
    10         }
    11         unordered_map<int, int> vectorcout;
    12         for(int la:LA){
    13             for(int lb:LB){
    14                 int vector=la-lb;
    15                 vectorcout[vector]++;
    16             }
    17         }
    18         for(auto k:vectorcout){
    19             res=max(res, k.second);
    20         }
    21         return res;
    22     }
    23 };
  • 相关阅读:
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
    Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
    Caused by: java.lang.ClassNotFoundException: ognl.PropertyAccessor
    利用DBLINK同步表数据库--老刘
    100万并发连接服务器笔记之1M并发连接目标达成
    模拟row cache lock
    redis读写性能测试
    Adobe RIA 开发工程师认证考试大纲
  • 原文地址:https://www.cnblogs.com/habibah-chang/p/12881278.html
Copyright © 2011-2022 走看看