Created
January 27, 2020 05:25
-
-
Save patilarpith/514acbf45ef4351a2ef8a7ce2663a402 to your computer and use it in GitHub Desktop.
Unique hats
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
#include <unordered_set> | |
using namespace std; | |
template <class T> | |
void print(vector<T> items) { | |
for(T item:items) | |
cout << item << " "; | |
cout << endl; | |
} | |
template <class T> | |
bool is_unique(vector<T> items) { | |
if(items.size() == 0) | |
return false; | |
unordered_set<T> myset; | |
for(T item:items) | |
myset.insert(item); | |
return (items.size() == myset.size()); | |
} | |
void print_unique_hats(vector<vector<int>> &men_hats, int row, int size, vector<int> &collection) { | |
// sanity | |
if(row == size) | |
return; | |
auto hats = men_hats[row]; | |
for(auto hat:hats) { | |
collection.push_back(hat); | |
if(collection.size() == size) | |
{ | |
if(is_unique<int>(collection)) | |
print<int>(collection); | |
} | |
else { | |
print_unique_hats(men_hats, row + 1, size, collection); | |
} | |
collection.pop_back(); | |
} | |
return; | |
} | |
int main() { | |
int total_men = 3; | |
vector<vector<int>> men_hats({{5, 100, 1}, {2}, {5, 100}}); | |
vector<int> collection; | |
print_unique_hats(men_hats, 0, total_men, collection); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment