{"id":4052,"date":"2018-09-19T23:27:38","date_gmt":"2018-09-20T06:27:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4052"},"modified":"2018-09-19T23:36:52","modified_gmt":"2018-09-20T06:36:52","slug":"leetcode-18-4sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-18-4sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 18. 4Sum"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>nums<\/code>\u00a0of\u00a0<em>n<\/em>\u00a0integers and an integer\u00a0<code>target<\/code>, are there elements\u00a0<em>a<\/em>,\u00a0<em>b<\/em>,\u00a0<em>c<\/em>, and\u00a0<em>d<\/em>\u00a0in\u00a0<code>nums<\/code>\u00a0such that\u00a0<em>a<\/em>\u00a0+\u00a0<em>b<\/em>\u00a0+\u00a0<em>c<\/em>\u00a0+\u00a0<em>d<\/em>\u00a0=\u00a0<code>target<\/code>? Find all unique quadruplets in the array which gives the sum of\u00a0<code>target<\/code>.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>The solution set must not contain duplicate quadruplets.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\">Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.\r\n\r\nA solution set is:\r\n[\r\n  [-1,  0, 0, 1],\r\n  [-2, -1, 1, 2],\r\n  [-2,  0, 0, 2]\r\n]<\/pre>\n<h1><strong>Solution 1: Sorting + Binary Search<\/strong><\/h1>\n<p>Time complexity: O(n^3 log n + klogk)<\/p>\n<p>Space complexity: O(k)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; fourSum(vector&lt;int&gt; &amp;num, int target) {\r\n    set&lt;vector&lt;int&gt;&gt; h; \r\n\r\n    sort(num.begin(), num.end());\r\n\r\n    int n = num.size();\r\n\r\n    for (int i = 0; i &lt; n; i++) {\r\n      for (int j = i + 1; j &lt; n; j++) {\r\n        for(int k = j + 1; k &lt; n; k++) {\r\n          int t = target - num[i] - num[j] - num[k];\r\n          if (t &lt; num[k]) break;\r\n          if (!std::binary_search(num.begin() + k + 1, num.end(), t)) continue;          \r\n          h.insert({num[i], num[j], num[k], t});          \r\n        }\r\n      }\r\n    }\r\n    return vector&lt;vector&lt;int&gt;&gt;(begin(h), end(h));\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ opt<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; fourSum(vector&lt;int&gt; &amp;num, int target) {        \r\n    sort(num.begin(), num.end());\r\n    if (target &gt; 0 &amp;&amp; target &gt; 4 * num.back()) return {};\r\n    if (target &lt; 0 &amp;&amp; target &lt; 4 * num.front()) return {};\r\n    \r\n    set&lt;vector&lt;int&gt;&gt; h;    \r\n    int n = num.size();\r\n\r\n    for (int i = 0; i &lt; n; i++) {   \r\n      for (int j = i + 1; j &lt; n; j++) {                \r\n        for(int k = j + 1; k &lt; n; k++) {\r\n          int t = target - num[i] - num[j] - num[k];\r\n          if (t &lt; num[k]) break;\r\n          if (!std::binary_search(num.begin() + k + 1, num.end(), t)) continue;          \r\n          h.insert({num[i], num[j], num[k], t});          \r\n        }           \r\n      }\r\n    }\r\n\r\n    return vector&lt;vector&lt;int&gt;&gt;(begin(h), end(h));\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Sorting + HashTable<\/strong><\/h1>\n<p>Time complexity: O(n^3 + klogk)<\/p>\n<p>Space complexity: O(n + k)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; fourSum(vector&lt;int&gt; &amp;num, int target) {        \r\n    sort(num.begin(), num.end());\r\n    if (target &gt; 0 &amp;&amp; target &gt; 4 * num.back()) return {};\r\n    if (target &lt; 0 &amp;&amp; target &lt; 4 * num.front()) return {};\r\n    \r\n    unordered_map&lt;int, int&gt; index;\r\n    for (int i = 0; i &lt; num.size(); ++i)\r\n      index[num[i]] = i;\r\n    \r\n    set&lt;vector&lt;int&gt;&gt; h;    \r\n    int n = num.size();\r\n\r\n    for (int i = 0; i &lt; n; i++) {   \r\n      for (int j = i + 1; j &lt; n; j++) {                \r\n        for(int k = j + 1; k &lt; n; k++) {\r\n          int t = target - num[i] - num[j] - num[k];\r\n          if (t &lt; num[k]) break;\r\n          auto it = index.find(t);\r\n          if (it == index.end() || it-&gt;second &lt;= k) continue;\r\n          h.insert({num[i], num[j], num[k], t});\r\n        }           \r\n      }\r\n    }\r\n\r\n    return vector&lt;vector&lt;int&gt;&gt;(begin(h), end(h));\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1-two-sum\/\">\u82b1\u82b1\u9171 LeetCode 1. Two Sum<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/167-two-sum-ii-input-array-is-sorted\/\">\u82b1\u82b1\u9171 167. Two Sum II &amp;#8211; Input array is sorted<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-653-two-sum-iv-input-is-a-bst\/\">\u82b1\u82b1\u9171 LeetCode 653. Two Sum IV &amp;#8211; Input is a BST<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-15-3sum\/\">\u82b1\u82b1\u9171 LeetCode 15. 3Sum<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-16-3sum-closest\/\">\u82b1\u82b1\u9171 LeetCode 16. 3Sum Closest<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0nums\u00a0of\u00a0n\u00a0integers and an integer\u00a0target, are there elements\u00a0a,\u00a0b,\u00a0c, and\u00a0d\u00a0in\u00a0nums\u00a0such that\u00a0a\u00a0+\u00a0b\u00a0+\u00a0c\u00a0+\u00a0d\u00a0=\u00a0target? Find all unique quadruplets in the array which gives the sum of\u00a0target. Note:&#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],"tags":[411,52,177],"class_list":["post-4052","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-4sum","tag-binary-search","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4052","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=4052"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4052\/revisions"}],"predecessor-version":[{"id":4056,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4052\/revisions\/4056"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}