Advertisement
Roszondas

ciw

Aug 18th, 2019
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. bool canIWin(int maxInt, int total) {
  11. if(total < maxInt)
  12. return true;
  13.  
  14. if ((maxInt + 1)*maxInt/2 < total)
  15. return false;
  16.  
  17. vector <int> numUsed (maxInt + 1);
  18. fill(numUsed.begin(), numUsed.end(), 0);
  19. this->maxInt = maxInt;
  20. map <vector <int>, bool> memory;
  21. return makeTurn(numUsed, total, memory);
  22. }
  23.  
  24. private:
  25. int maxInt;
  26.  
  27. bool makeTurn(vector <int> &used, int total, map <vector <int>, bool> &memory) {
  28. bool ret = false;
  29.  
  30. auto it = memory.find(used);
  31. if (it != memory.end()) {
  32. return it->second;
  33. }
  34.  
  35. if (total <= 0) {
  36. return false;
  37. }
  38.  
  39. for (int i = 1; i <= maxInt; i++) {
  40. if (used[i])
  41. continue;
  42.  
  43. used[i] = 1;
  44.  
  45. ret = !makeTurn(used, total - i, memory);
  46.  
  47. used[i] = 0;
  48.  
  49. if(ret)
  50. break;
  51. }
  52. memory.insert(pair<vector<int>, bool>(used, ret));
  53. return ret;
  54. }
  55.  
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement