{"id":7893,"date":"2021-01-02T23:35:38","date_gmt":"2021-01-03T07:35:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7893"},"modified":"2021-01-02T23:37:34","modified_gmt":"2021-01-03T07:37:34","slug":"leetcode-1711-count-good-meals","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1711-count-good-meals\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1711. Count Good Meals"},"content":{"rendered":"\n<p>A&nbsp;<strong>good meal<\/strong>&nbsp;is a meal that contains&nbsp;<strong>exactly two different food items<\/strong>&nbsp;with a sum of deliciousness equal to a power of two.<\/p>\n\n\n\n<p>You can pick&nbsp;<strong>any<\/strong>&nbsp;two different foods to make a good meal.<\/p>\n\n\n\n<p>Given an array of integers&nbsp;<code>deliciousness<\/code>&nbsp;where&nbsp;<code>deliciousness[i]<\/code>&nbsp;is the deliciousness of the&nbsp;<code>i<sup>\u200b\u200b\u200b\u200b\u200b\u200bth<\/sup>\u200b\u200b\u200b\u200b<\/code>\u200b\u200b\u200b\u200b item of food, return&nbsp;<em>the number of different&nbsp;<strong>good meals<\/strong>&nbsp;you can make from this list modulo<\/em>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p>Note that items with different indices are considered different even if they have the same deliciousness value.<\/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> deliciousness = [1,3,5,7,9]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>The good meals are (1,3), (1,7), (3,5) and, (7,9).\nTheir respective sums are 4, 8, 8, and 16, all of which are powers of 2.\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> deliciousness = [1,1,1,3,3,3,7]\n<strong>Output:<\/strong> 15\n<strong>Explanation: <\/strong>The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= deliciousness.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= deliciousness[i] &lt;= 2<sup>20<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Same idea as LeetCode 1: Two Sum<\/p>\n\n\n\n<p>Use a hashtable to store the occurrences of all the numbers added so far. For a  new number x, check all possible 2^i &#8211; x. ans += freq[2^i &#8211; x] 0 &lt;= i &lt;= 21<\/p>\n\n\n\n<p>Time complexity: O(22n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int countPairs(vector<int>& A) {\n    constexpr int kMod = 1e9 + 7;\n    unordered_map<int, int> m;    \n    long ans = 0;\n    for (int x : A) {\n      for (int t = 1; t <= 1 << 21; t *= 2) {\n        auto it = m.find(t - x);\n        if (it != end(m)) ans += it->second;\n      }\n      ++m[x];\n    }\n    return ans % kMod;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def countPairs(self, deliciousness: List[int]) -> int:\n    sums = [1<<i for i in range(22)]\n    m = defaultdict(int)\n    ans = 0\n    for x in deliciousness:\n      for t in sums:\n        if t - x in m: ans += m[t - x]\n      m[x] += 1\n    return ans % (10**9 + 7)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;good meal&nbsp;is a meal that contains&nbsp;exactly two different food items&nbsp;with a sum of deliciousness equal to a power of two. You can pick&nbsp;any&nbsp;two different foods&#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,177,269,492,208],"class_list":["post-7893","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-pairs","tag-target-sum","tag-two-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7893","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=7893"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7893\/revisions"}],"predecessor-version":[{"id":7896,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7893\/revisions\/7896"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}