-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
66 lines (53 loc) · 2.02 KB
/
Copy pathsolution.cpp
File metadata and controls
66 lines (53 loc) · 2.02 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public:
vector<int> stableMarriage(vector<vector<int>> &men, vector<vector<int>> &women) {
int n = men.size();
// rank[w][m] = preference rank of man m for woman w
// Smaller value means woman prefers that man more
vector<vector<int>> rank(n, vector<int>(n));
for (int w = 0; w < n; w++) {
for (int pos = 0; pos < n; pos++) {
rank[w][women[w][pos]] = pos;
}
}
// womanPartner[w] = current man matched with woman w
vector<int> womanPartner(n, -1);
// result[m] = current woman matched with man m
vector<int> result(n, -1);
// nextProposal[m] = next woman index in man's preference list
vector<int> nextProposal(n, 0);
queue<int> freeMen;
// Initially all men are free
for (int i = 0; i < n; i++) {
freeMen.push(i);
}
while (!freeMen.empty()) {
int man = freeMen.front();
freeMen.pop();
// Get the next woman this man wants to propose to
int woman = men[man][nextProposal[man]];
nextProposal[man]++;
// If woman is free, match them
if (womanPartner[woman] == -1) {
womanPartner[woman] = man;
result[man] = woman;
}
else {
int currentMan = womanPartner[woman];
// If woman prefers new man over current partner
if (rank[woman][man] < rank[woman][currentMan]) {
womanPartner[woman] = man;
result[man] = woman;
// Old partner becomes free again
result[currentMan] = -1;
freeMen.push(currentMan);
}
else {
// Woman rejects the proposal
freeMen.push(man);
}
}
}
return result;
}
};