Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <vector>
  2. #include <set>
  3.  
  4. //Just a brute force recursive solution, to be used as a first step in finding the optimal solution, i.e. by adding memoization
  5. //and then possibly flipping into a table-based bottom-up solution
  6. namespace with_multiset
  7. {
  8. void make_change (int amount, std::vector<int> const &coins, std::set<std::multiset<int>> &changes, std::multiset<int> partial = {})
  9. {
  10. if (amount == 0)
  11. {
  12. changes.insert(partial);
  13. return;
  14. }
  15.  
  16. int res = 0;
  17. for (auto c : coins)
  18. {
  19. if (c <= amount)
  20. {
  21. auto where = partial.insert(c);
  22. make_change(amount - c, coins, changes, partial);
  23. partial.erase(where);
  24. }
  25. }
  26. }
  27. std::set<std::multiset<int>> make_change (int amount, std::vector<int> const &coins)
  28. {
  29. std::set<std::multiset<int>> changes;
  30. make_change(amount, coins, changes);
  31. return changes;
  32.  
  33. }
  34. }//namespace with_multiset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement