forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1434.Number-of-Ways-to-Wear-Different-Hats-to-Each-Other.cpp
- Loading branch information
1 parent
0bdf186
commit 969e677
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...Different-Hats-to-Each-Other/1434.Number-of-Ways-to-Wear-Different-Hats-to-Each-Other.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Solution { | ||
public: | ||
int numberWays(vector<vector<int>>& hats) | ||
{ | ||
int n = hats.size(); | ||
unordered_map<int,vector<int>>Map; | ||
for (int i=0; i<hats.size(); i++) | ||
for (int j=0; j<hats[i].size(); j++) | ||
Map[hats[i][j]].push_back(i); | ||
|
||
vector<long>dp(1<<n, 0); | ||
dp[0] = 1; | ||
for (int p: Map[1]) | ||
dp[1<<p] = 1; | ||
long M = 1e9+7; | ||
|
||
for (int i=2; i<=40; i++) | ||
{ | ||
auto dp2=dp; | ||
|
||
for (int set = 0; set<(1<<n); set++) | ||
{ | ||
for (int p: Map[i]) | ||
{ | ||
if (((set>>p)&1)==0) | ||
{ | ||
dp2[set+(1<<p)] += dp[set]; | ||
dp2[set+(1<<p)]%=M; | ||
} | ||
} | ||
} | ||
|
||
dp = dp2; | ||
} | ||
|
||
return dp[(1<<n)-1]; | ||
} | ||
}; |