[LeetCode] Maximum Number of Accepted Invitations

1820. Maximum Number of Accepted Invitations

There are m boys and n girls in a class attending an upcoming party.

You are given an m x n integer matrix grid, where grid[i][j] equals 0 or 1. If grid[i][j] == 1, then that means the ith boy can invite the jth girl to the party. A boy can invite at most one girl, and a girl can accept at most one invitation from a boy.

Return the maximum possible number of accepted invitations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
bool dfs(int i, vector<vector<int>>& g, vector<int>& matching, vector<bool>& ask) {
for(int j = 0; j < g[i].size(); j++) {
if(g[i][j] == 0 or ask[j]) continue;
ask[j] = true;
if(matching[j] == -1 or dfs(matching[j],g,matching,ask)) {
matching[j] = i;
return true;
}
}


return false;
}
public:
int maximumInvitations(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size(), res = 0;
vector<int> matching(m, -1);
for(int i = 0; i < n; i++)
res += dfs(i,grid,matching,vector<bool>(m) = {});

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/28/PS/LeetCode/maximum-number-of-accepted-invitations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.