{"id":4180,"date":"2018-10-13T20:01:25","date_gmt":"2018-10-14T03:01:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4180"},"modified":"2018-10-19T23:16:46","modified_gmt":"2018-10-20T06:16:46","slug":"leetcode-923-3sum-with-multiplicity","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-923-3sum-with-multiplicity\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 923. 3Sum With Multiplicity"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 923. 3Sum With Multiplicity - \u5237\u9898\u627e\u5de5\u4f5c EP227\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/GYIrV9yxgeU?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<h1><strong>Problem<\/strong><\/h1>\n<p>Given an integer array\u00a0<code>A<\/code>, and an integer\u00a0<code>target<\/code>, return the number of\u00a0tuples\u00a0<code>i, j, k<\/code>\u00a0 such that\u00a0<code>i &lt; j &lt; k<\/code>\u00a0and\u00a0<code>A[i] + A[j] + A[k] == target<\/code>.<\/p>\n<p><strong>As the answer can be very large, return it modulo\u00a0<code>10^9 + 7<\/code><\/strong>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-1-1\">[1,1,2,2,3,3,4,4,5,5]<\/span>, target = <span id=\"example-input-1-2\">8<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">20<\/span>\r\n<strong>Explanation: <\/strong>\r\nEnumerating by the values (A[i], A[j], A[k]):\r\n(1, 2, 5) occurs 8 times;\r\n(1, 3, 4) occurs 8 times;\r\n(2, 2, 4) occurs 2 times;\r\n(2, 3, 3) occurs 2 times.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-2-1\">[1,1,2,2,2,2]<\/span>, target = <span id=\"example-input-2-2\">5<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">12<\/span>\r\n<strong>Explanation: <\/strong>\r\nA[i] = 1, A[j] = A[k] = 2 occurs 12 times:\r\nWe choose one 1 from [1,1] in 2 ways,\r\nand two 2s from [2,2,2,2] in 6 ways.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>3 &lt;= A.length &lt;= 3000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 100<\/code><\/li>\n<li><code>0 &lt;= target &lt;= 300<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Math \/ Combination<\/strong><\/h1>\n<p>Time complexity: O(n + |target|^2)<\/p>\n<p>Space complexity: O(|target|)<\/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, runtime: 4 ms\r\nclass Solution {\r\npublic:\r\n  int threeSumMulti(vector&lt;int&gt;&amp; A, int target) {    \r\n    constexpr int kMaxN = 100;\r\n    constexpr int kMod = 1e9 + 7;\r\n    vector&lt;long&gt; c(kMaxN + 1, 0);\r\n    for (int a : A) ++c[a];\r\n    long ans = 0;\r\n    for (int i = 0; i &lt;= target; ++i) {\r\n      for (int j = i; j &lt;= target; ++j) {\r\n        const int k = target - i - j;\r\n        if (k &lt; 0 || k &gt;= c.size() || k &lt; j) continue;\r\n        if (!c[i] || !c[j] || !c[k]) continue;\r\n        if (i == j &amp;&amp; j == k)\r\n          ans += (c[i] - 2) * (c[i] - 1) * c[i] \/ 6;\r\n        else if (i == j &amp;&amp; j != k)\r\n          ans += c[i] * (c[i] - 1) \/ 2 * c[k];\r\n        else if (i != j &amp;&amp; j == k)\r\n          ans += c[i] * (c[j] - 1) * c[j] \/ 2;\r\n        else\r\n          ans += c[i] * c[j] * c[k];        \r\n      }\r\n    }\r\n    return ans % kMod;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an integer array\u00a0A, and an integer\u00a0target, return the number of\u00a0tuples\u00a0i, j, k\u00a0 such that\u00a0i &lt; j &lt; k\u00a0and\u00a0A[i] + A[j] + A[k] ==&#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,49],"tags":[122,31,177],"class_list":["post-4180","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-math","tag-combination","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4180","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=4180"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4180\/revisions"}],"predecessor-version":[{"id":4201,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4180\/revisions\/4201"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}