Advertisement
SalmaYasser

Untitled

Jan 4th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. class Solution {
  2. private:
  3. int sigh(vector <int> &arr , int idx)
  4. {
  5. int sz = arr.size();
  6. while ((idx < sz) && (arr[idx] == 0))
  7. {
  8. idx++;
  9. }
  10.  
  11. int res = INT_MAX;
  12.  
  13. for (int i = idx + 1; i < sz; i++)
  14. {
  15. if ((arr[idx] ^ arr[i]) < 0)
  16. {
  17. arr[i] += arr[idx];
  18. res = min(res, sigh(arr, idx + 1) + 1);
  19. arr[i] -= arr[idx];
  20. }
  21. }
  22.  
  23. return (res == INT_MAX) ? 0 : res;
  24. }
  25.  
  26. public:
  27. int minTransfers(vector<vector<int>>& transactions) {
  28. unordered_map<int, int> all;
  29.  
  30. for (const vector<int> &x : transactions) {
  31. all[x[0]] -= x[2];
  32. all[x[1]] += x[2];
  33. }
  34.  
  35. unordered_multiset<int> fin;
  36. int same = 0;
  37. for (const pair<int, int> &x: all)
  38. {
  39.  
  40. if (x.second != 0)
  41. {
  42. auto it = fin.find(-x.second);
  43. if (it != fin.end())
  44. {
  45. fin.erase(it);
  46. same++;
  47. }
  48. else
  49. {
  50. fin.insert(x.second);
  51. }
  52. }
  53. }
  54.  
  55. vector<int> arr (fin.begin(), fin.end());
  56. return same + sigh (arr, 0);
  57. }
  58. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement