{"id":1986,"date":"2018-03-05T21:28:31","date_gmt":"2018-03-06T05:28:31","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1986"},"modified":"2018-03-06T08:48:52","modified_gmt":"2018-03-06T16:48:52","slug":"leetcode-454-4sum-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-454-4sum-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 454. 4Sum II"},"content":{"rendered":"<div class=\"question-description\">\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f604\u4e2a\u7ec4\u6570\uff1aA, B, C, D\u3002\u95ee\u6709\u591a\u5c11\u7ec4\u7ec4\u5408\u7684A[i] + B[j] + C[k] + D[l] \u7684\u548c\u4e3a0\u3002<\/p>\n<p>Given four lists A, B, C, D of integer values, compute how many tuples\u00a0<code>(i, j, k, l)<\/code>\u00a0there are such that\u00a0<code>A[i] + B[j] + C[k] + D[l]<\/code>is zero.<\/p>\n<p>To make problem a bit easier, all A, B, C, D have same length of N where 0 \u2264 N \u2264 500. All integers are in the range of -2<sup>28<\/sup>\u00a0to 2<sup>28<\/sup>\u00a0&#8211; 1 and the result is guaranteed to be at most 2<sup>31<\/sup>\u00a0&#8211; 1.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">Input:\r\nA = [ 1, 2]\r\nB = [-2,-1]\r\nC = [-1, 2]\r\nD = [ 0, 2]\r\n\r\nOutput:\r\n2\r\n\r\nExplanation:\r\nThe two tuples are:\r\n1. (0, 0, 0, 1) -&gt; A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0\r\n2. (1, 1, 0, 0) -&gt; A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0\r\n<\/pre>\n<\/div>\n<p><strong>Solution 1: HashTable<\/strong><\/p>\n<p>Split the arrays into two groups: (A, B), (C, D)<\/p>\n<p>Create the Minkowski sum of two groups and count the occurrence of each element.<\/p>\n<p>e.g. A = [1, 2, 3], B = [0, -1, 1]<\/p>\n<p>Minkowski sum(A, B) SAB= [0, 1, 2, 3, 4] =&gt; [0:1, 1:2, 2:3, 3:2, 4:1]<\/p>\n<p>for each element s in SAB, check whether -s is in Minkowski sum(C, D) SCD<\/p>\n<p>ans = sum(SAB[s] * SCD[-s]), for all s, that s in SAB and -s in SCD<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n^2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 217 ms\r\nclass Solution {\r\npublic:\r\n  int fourSumCount(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B, vector&lt;int&gt;&amp; C, vector&lt;int&gt;&amp; D) {\r\n    unordered_map&lt;int, int&gt; sums;\r\n    for (int a : A)\r\n      for (int b : B)\r\n        ++sums[a + b];\r\n    int ans = 0;\r\n    for (int c : C)\r\n      for (int d : D) {\r\n        auto it = sums.find(- c - d);\r\n        if (it != sums.end()) ans += it-&gt;second;          \r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 672 ms\r\n\"\"\"\r\nclass Solution:\r\n  def fourSumCount(self, A, B, C, D):\r\n    s = collections.Counter([a + b for a in A for b in B])\r\n    return sum(s[-c - d] for c in C for d in D)<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1-two-sum\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 1. Two Sum &amp;#8211; \u82b1\u82b1\u9171<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f604\u4e2a\u7ec4\u6570\uff1aA, B, C, D\u3002\u95ee\u6709\u591a\u5c11\u7ec4\u7ec4\u5408\u7684A[i] + B[j] + C[k] + D[l] \u7684\u548c\u4e3a0\u3002 Given four lists A, B, C, D of integer values, compute how many tuples\u00a0(i,&#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,62],"class_list":["post-1986","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1986","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=1986"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"predecessor-version":[{"id":1993,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions\/1993"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}