{"id":6517,"date":"2020-03-21T16:20:50","date_gmt":"2020-03-21T23:20:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6517"},"modified":"2020-03-21T22:29:17","modified_gmt":"2020-03-22T05:29:17","slug":"leetcode-1385-find-the-distance-value-between-two-arrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-1385-find-the-distance-value-between-two-arrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1385. Find the Distance Value Between Two Arrays"},"content":{"rendered":"\n<p>Given two integer arrays&nbsp;<code>arr1<\/code>&nbsp;and&nbsp;<code>arr2<\/code>, and the integer&nbsp;<code>d<\/code>,&nbsp;<em>return the distance value between the two&nbsp;arrays<\/em>.<\/p>\n\n\n\n<p>The distance value is defined as the number of elements&nbsp;<code>arr1[i]<\/code>&nbsp;such that there is not any element&nbsp;<code>arr2[j]<\/code>&nbsp;where&nbsp;<code>|arr1[i]-arr2[j]| &lt;= d<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \nFor arr1[0]=4 we have: \n|4-10|=6 &gt; d=2 \n|4-9|=5 &gt; d=2 \n|4-1|=3 &gt; d=2 \n|4-8|=4 &gt; d=2 \nFor arr1[1]=5 we have: \n|5-10|=5 &gt; d=2 \n|5-9|=4 &gt; d=2 \n|5-1|=4 &gt; d=2 \n|5-8|=3 &gt; d=2\nFor arr1[2]=8 we have:\n<strong>|8-10|=2 &lt;= d=2<\/strong>\n<strong>|8-9|=1 &lt;= d=2<\/strong>\n|8-1|=7 &gt; d=2\n<strong>|8-8|=0 &lt;= d=2<\/strong>\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> arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3\n<strong>Output:<\/strong> 2\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> arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr1.length, arr2.length &lt;= 500<\/code><\/li><li><code>-10^3 &lt;= arr1[i], arr2[j] &lt;= 10^3<\/code><\/li><li><code>0 &lt;= d &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1:  All pairs<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(1)<\/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 findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {    \n    int ans = 0;\n    for (int x : arr1) {\n      bool flag = true;\n      for (int y : arr2)\n        if (abs(x - y) <= d) {\n          flag = false;\n          break;\n        }\n      ans += flag;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:\n    return sum(all(abs(x - y) > d for y in arr2) for x in arr1)\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Sort arr1 in ascending order and sort arr2 in descending order.<br>Time complexity: O(mlogm + nlogn + m + n)<br>Space complexity: O(1)<\/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 findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {    \n    sort(begin(arr1), end(arr1));\n    sort(rbegin(arr2), rend(arr2));\n    int ans = 0;\n    for (int a : arr1) {\n      while (arr2.size() && arr2.back() < a - d)\n        arr2.pop_back();\n      ans += arr2.empty() || arr2.back() > a + d;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 3: Binary Search<\/strong><\/h2>\n\n\n\n<p>Sort arr2 in ascending order. and do two binary searches for each element to determine the range of [a-d, a+d], if that range is empty we increase the counter<\/p>\n\n\n\n<p>Time complexity: O(mlogm + nlogm)<br>Space complexity: O(1)<\/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 findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {    \n    sort(begin(arr2), end(arr2));\n    int ans = 0;\n    for (int a : arr1) {\n      auto it1 = lower_bound(begin(arr2), end(arr2), a - d);\n      auto it2 = upper_bound(begin(arr2), end(arr2), a + d);\n      if (it1 == it2) ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two integer arrays&nbsp;arr1&nbsp;and&nbsp;arr2, and the integer&nbsp;d,&nbsp;return the distance value between the two&nbsp;arrays. The distance value is defined as the number of elements&nbsp;arr1[i]&nbsp;such that there&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[52,106,222,23,175],"class_list":["post-6517","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-binary-search","tag-brute-force","tag-easy","tag-sort","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6517","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=6517"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6517\/revisions"}],"predecessor-version":[{"id":6522,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6517\/revisions\/6522"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}