-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumNumberofAchievableTransferRequests.cpp
More file actions
49 lines (46 loc) · 1.32 KB
/
maximumNumberofAchievableTransferRequests.cpp
File metadata and controls
49 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Source: https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/
// Author: Miao Zhang
// Date: 2021-05-20
class Solution {
public:
int maximumRequests(int n, vector<vector<int>>& requests) {
int selfloop = 0;
vector<int> out(n, 0), in(n, 0);
for (auto& r: requests) {
if (r[0] != r[1]) {
out[r[0]]++;
in[r[1]]++;
} else {
selfloop++;
}
}
vector<vector<int>> req;
for (auto& r: requests) {
if (r[0] == r[1] || !out[r[0]] || !in[r[0]] || !out[r[0]] || !out[r[1]])
continue;
req.push_back(r);
}
int r = req.size();
int res = 0;
auto check = [&] (int state) {
vector<int> in(n, 0), out(n, 0);
int idx = 0;
int ret = 0;
while (state) {
if (state & 1) {
out[req[idx][0]]++;
in[req[idx][1]]++;
ret++;
}
state >>= 1;
idx++;
}
if (in != out) return -1;
return ret;
};
for (int i = (1 << r) - 1; i > 0; i--) {
res = max(res, check(i));
}
return res + selfloop;
}
};