Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- private:
- int sigh(vector <int> &arr , int idx)
- {
- int sz = arr.size();
- while ((idx < sz) && (arr[idx] == 0))
- {
- idx++;
- }
- int res = INT_MAX;
- for (int i = idx + 1; i < sz; i++)
- {
- if ((arr[idx] ^ arr[i]) < 0)
- {
- arr[i] += arr[idx];
- res = min(res, sigh(arr, idx + 1) + 1);
- arr[i] -= arr[idx];
- }
- }
- return (res == INT_MAX) ? 0 : res;
- }
- public:
- int minTransfers(vector<vector<int>>& transactions) {
- unordered_map<int, int> all;
- for (const vector<int> &x : transactions) {
- all[x[0]] -= x[2];
- all[x[1]] += x[2];
- }
- unordered_multiset<int> fin;
- int same = 0;
- for (const pair<int, int> &x: all)
- {
- if (x.second != 0)
- {
- auto it = fin.find(-x.second);
- if (it != fin.end())
- {
- fin.erase(it);
- same++;
- }
- else
- {
- fin.insert(x.second);
- }
- }
- }
- vector<int> arr (fin.begin(), fin.end());
- return same + sigh (arr, 0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement