{"id":6691,"date":"2020-05-03T20:23:29","date_gmt":"2020-05-04T03:23:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6691"},"modified":"2020-05-08T08:57:26","modified_gmt":"2020-05-08T15:57:26","slug":"leetcode-1434-number-of-ways-to-wear-different-hats-to-each-other","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1434-number-of-ways-to-wear-different-hats-to-each-other\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1434. Number of Ways to Wear Different Hats to Each Other"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;people&nbsp;and 40 types of hats labeled from 1 to 40.<\/p>\n\n\n\n<p>Given a list of list of integers&nbsp;<code>hats<\/code>, where&nbsp;<code>hats[i]<\/code>&nbsp;is a list of all hats preferred&nbsp;by the&nbsp;<code>i-th<\/code>&nbsp;person.<\/p>\n\n\n\n<p>Return the number of ways that the n people wear different hats to each other.<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;<code>10^9 + 7<\/code>.<\/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> hats = [[3,4],[4,5],[5]]\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>There is only one way to choose hats given the conditions. \nFirst person choose hat 3, Second person choose hat 4 and last one hat 5.<\/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> hats = [[3,5,1],[3,5]]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>There are 4 ways to choose hats\n(3,5), (5,3), (1,3) and (1,5)\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]\n<strong>Output:<\/strong> 24\n<strong>Explanation: <\/strong>Each person can choose hats labeled from 1 to 4.\nNumber of Permutations of (1,2,3,4) = 24.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]]\n<strong>Output:<\/strong> 111\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == hats.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<\/code><\/li><li><code>1 &lt;= hats[i].length &lt;= 40<\/code><\/li><li><code>1 &lt;= hats[i][j] &lt;= 40<\/code><\/li><li><code>hats[i]<\/code>&nbsp;contains a list of&nbsp;<strong>unique<\/strong>&nbsp;integers.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i][j] := # of ways using first i hats, j is the bit mask of people wearing hats.<\/p>\n\n\n\n<p>e.g. dp[3][101] == # of ways using first 3 hats that people 1 and 3 are wearing hats.<\/p>\n\n\n\n<p>init dp[0][0] = 1<\/p>\n\n\n\n<p>dp[i][mask |  (1 &lt;&lt; p)] = dp[i-1][mask | (1 &lt;&lt; p)] + dp[i-1][mask], where people p prefers hats i.<\/p>\n\n\n\n<p>ans: dp[nHat][1&#8230;1] <\/p>\n\n\n\n<p>Time complexity: O(2^n * h * n)<br>Space complexity: O(2^n * h) -&gt; O(2^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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberWays(vector<vector<int>>& hats) {\n    constexpr int kHat = 40;\n    constexpr int kMod = 1e9 + 7;\n    const int n = hats.size();    \n    vector<vector<int>> people(kHat + 1); \/\/ hat -> {people}\n    for (int i = 0; i < n; ++i)\n      for (int hat : hats[i])\n        people[hat].push_back(i);\n    \/\/ dp[i][j] := # of ways using first i hats where j is bit mask of people wearing hats.\n    vector<vector<int>> dp(kHat + 1, vector<int>(1 << n));\n    dp[0][0] = 1;\n    \n    for (int i = 1; i <= kHat; ++i) {\n      dp[i] = dp[i - 1];\n      for (int mask = (1 << n) - 1; mask >= 0; --mask)\n        for (int p : people[i]) {\n          if (mask & (1 << p)) continue;\n          dp[i][mask | (1 << p)] += dp[i - 1][mask];\n          dp[i][mask | (1 << p)] %= kMod;          \n        }\n    }\n    return dp.back().back();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>O(2^n) memory<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberWays(vector<vector<int>>& hats) {\n    constexpr int kHat = 40;\n    constexpr int kMod = 1e9 + 7;\n    const int n = hats.size();    \n    vector<vector<int>> people(kHat + 1); \/\/ hat -> {people}\n    for (int i = 0; i < n; ++i)\n      for (int hat : hats[i])\n        people[hat].push_back(i);\n    \/\/ dp([i])[j] := # of ways using first i hats where j is bit mask of people wearing hat.\n    vector<int> dp(1 << n);\n    dp[0] = 1;\n    \n    for (int i = 1; i <= kHat; ++i)      \n      for (int mask = (1 << n) - 1; mask >= 0; --mask)\n        for (int p : people[i]) {\n          if (mask & (1 << p)) continue;\n          dp[mask | (1 << p)] += dp[mask];\n          dp[mask | (1 << p)] %= kMod;          \n      }\n    return dp.back();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;people&nbsp;and 40 types of hats labeled from 1 to 40. Given a list of list of integers&nbsp;hats, where&nbsp;hats[i]&nbsp;is a list of all hats preferred&nbsp;by&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217,589],"class_list":["post-6691","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-state-compression","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6691","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=6691"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6691\/revisions"}],"predecessor-version":[{"id":6731,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6691\/revisions\/6731"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}