{"id":710,"date":"2017-10-29T13:14:10","date_gmt":"2017-10-29T20:14:10","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=710"},"modified":"2018-04-19T08:28:54","modified_gmt":"2018-04-19T15:28:54","slug":"leetcode-719-find-k-th-smallest-pair-distance","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/divide-and-conquer\/leetcode-719-find-k-th-smallest-pair-distance\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 719. Find K-th Smallest Pair Distance"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 719. Find K-th Smallest Pair Distance - \u5237\u9898\u627e\u5de5\u4f5c EP99\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/WHfljqX61Y8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u8fd4\u56de\u6240\u6709\u6570\u5bf9\u4e2d\uff0c\u7edd\u5bf9\u503c\u5dee\u7b2ck\u5c0f\u7684\u503c\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an integer array, return the k-th smallest\u00a0<b>distance<\/b>\u00a0among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">Input:\r\nnums = [1,3,1]\r\nk = 1\r\nOutput: 0 \r\nExplanation:\r\nHere are all the pairs:\r\n(1,3) -&gt; 2\r\n(1,1) -&gt; 0\r\n(3,1) -&gt; 2\r\nThen the 1st smallest distance pair is (1,1), and its distance is 0.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>2 &lt;= len(nums) &lt;= 10000<\/code>.<\/li>\n<li><code>0 &lt;= nums[i] &lt; 1000000<\/code>.<\/li>\n<li><code>1 &lt;= k &lt;= len(nums) * (len(nums) - 1) \/ 2<\/code>.<\/li>\n<\/ol>\n<p><strong>Idea<\/strong><\/p>\n<p>Bucket sort<\/p>\n<p>Binary search \/ dp<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-720\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-719\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/719-ep99-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>C++ \/ binary search<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    int smallestDistancePair(vector&lt;int&gt;&amp; nums, int k) {\r\n        std::sort(nums.begin(), nums.end());\r\n        int n = nums.size();\r\n        int l = 0;\r\n        int r = nums.back() - nums.front();\r\n        while (l &lt;= r) {\r\n            int cnt = 0;\r\n            int j = 0;\r\n            int m = l + (r - l) \/ 2;\r\n            for (int i = 0; i &lt; n; ++i) {\r\n                while (j &lt; n &amp;&amp; nums[j] - nums[i] &lt;= m) ++j;\r\n                cnt += j - i - 1;\r\n            }\r\n            cnt &gt;= k ? r = m - 1 : l = m + 1;\r\n        }        \r\n        return l;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ \/ bucket sort w\/ vector O(n^2)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 549 ms\r\nclass Solution {\r\npublic:\r\n    int smallestDistancePair(vector&lt;int&gt;&amp; nums, int k) {\r\n        std::sort(nums.begin(), nums.end());\r\n        const int N = nums.back();\r\n        vector&lt;int&gt; count(N + 1, 0);        \r\n        const int n = nums.size();\r\n        for (int i = 0; i &lt; n; ++i)\r\n            for (int j = i + 1; j &lt; n; ++j)\r\n               ++count[nums[j] - nums[i]];\r\n        for (int i = 0; i &lt;= N; ++i) {\r\n            k -= count[i];\r\n            if (k &lt;= 0) return i;\r\n        }\r\n        return 0;\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-786-k-th-smallest-prime-fraction\/\">\u82b1\u82b1\u9171 LeetCode 786. K-th Smallest Prime Fraction<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-378-kth-smallest-element-in-a-sorted-matrix\/\">\u82b1\u82b1\u9171 LeetCode 378. Kth Smallest Element in a Sorted Matrix<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u8fd4\u56de\u6240\u6709\u6570\u5bf9\u4e2d\uff0c\u7edd\u5bf9\u503c\u5dee\u7b2ck\u5c0f\u7684\u503c\u3002 Problem: Given an integer array, return the k-th smallest\u00a0distance\u00a0among all the pairs. The distance of a pair (A, B) is defined as the absolute&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,43,165],"tags":[52,115,130,148],"class_list":["post-710","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-divide-and-conquer","category-hard","tag-binary-search","tag-bucket","tag-distance","tag-pair","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/710","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=710"}],"version-history":[{"count":15,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/710\/revisions"}],"predecessor-version":[{"id":2559,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/710\/revisions\/2559"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}