Skip to content

Instantly share code, notes, and snippets.

@patilarpith
Created January 27, 2020 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patilarpith/514acbf45ef4351a2ef8a7ce2663a402 to your computer and use it in GitHub Desktop.
Save patilarpith/514acbf45ef4351a2ef8a7ce2663a402 to your computer and use it in GitHub Desktop.
Unique hats
#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