{"id":9204,"date":"2021-12-23T12:45:49","date_gmt":"2021-12-23T20:45:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9204"},"modified":"2021-12-23T12:49:04","modified_gmt":"2021-12-23T20:49:04","slug":"leetcode-835-image-overlap","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-835-image-overlap\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 835. Image Overlap"},"content":{"rendered":"\n<p>You are given two images,&nbsp;<code>img1<\/code>&nbsp;and&nbsp;<code>img2<\/code>, represented as binary, square matrices of size&nbsp;<code>n x n<\/code>. A binary matrix has only&nbsp;<code>0<\/code>s and&nbsp;<code>1<\/code>s as values.<\/p>\n\n\n\n<p>We&nbsp;<strong>translate<\/strong>&nbsp;one image however we choose by sliding all the&nbsp;<code>1<\/code>&nbsp;bits left, right, up, and\/or down any number of units. We then place it on top of the other image. We can then calculate the&nbsp;<strong>overlap<\/strong>&nbsp;by counting the number of positions that have a&nbsp;<code>1<\/code>&nbsp;in&nbsp;<strong>both<\/strong>&nbsp;images.<\/p>\n\n\n\n<p>Note also that a translation does&nbsp;<strong>not<\/strong>&nbsp;include any kind of rotation. Any&nbsp;<code>1<\/code>&nbsp;bits that are translated outside of the matrix borders are erased.<\/p>\n\n\n\n<p>Return&nbsp;<em>the largest possible overlap<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/09\/overlap1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> We translate img1 to right by 1 unit and down by 1 unit.\n<img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/09\/overlap_step1.jpg\">\nThe number of positions that have a 1 in both images is 3 (shown in red).\n<img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/09\/overlap_step2.jpg\">\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> img1 = [[1]], img2 = [[1]]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> img1 = [[0]], img2 = [[0]]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == img1.length == img1[i].length<\/code><\/li><li><code>n == img2.length == img2[i].length<\/code><\/li><li><code>1 &lt;= n &lt;= 30<\/code><\/li><li><code>img1[i][j]<\/code>&nbsp;is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><li><code>img2[i][j]<\/code>&nbsp;is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable of offsets<\/strong><\/h2>\n\n\n\n<p>Enumerate all pairs of 1 cells (x1, y1) (x2, y2), the key \/ offset will be ((x1-x2), (y1-y2)), i.e how should we shift the image to have those two cells overlapped. Use a counter to find the most common\/best offset.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>4<\/sup>) Note: this is the same as brute force \/ simulation method if the matrix is dense.<br>Space complexity: O(n<sup>2<\/sup>)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int largestOverlap(vector<vector<int>>& img1, vector<vector<int>>& img2) {\n    const int n = img1.size();\n    unordered_map<int, int> m;\n    for (int y1 = 0; y1 < n; ++y1)\n      for (int x1 = 0; x1 < n; ++x1)\n        if (img1[y1][x1])\n          for (int y2 = 0; y2 < n; ++y2) \n            for (int x2 = 0; x2 < n; ++x2)\n              if (img2[y2][x2])                 \n                ++m[(x1 - x2) * 100 + (y1 - y2)];\n    int ans = 0;\n    for (const auto [key, count] : m)\n      ans = max(ans, count);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two images,&nbsp;img1&nbsp;and&nbsp;img2, represented as binary, square matrices of size&nbsp;n x n. A binary matrix has only&nbsp;0s and&nbsp;1s as values. We&nbsp;translate&nbsp;one image however&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,216,177],"class_list":["post-9204","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-matrix","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9204"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9204\/revisions"}],"predecessor-version":[{"id":9208,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9204\/revisions\/9208"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}