- Difficulty: Medium
- Tags: LeetCode, Medium, Array, Backtracking, Matrix, leetcode-1820, Graph, O(m * n * sqrt(m + n)), O(m + n), 🔒, Hopcroft-Karp Bipartite Matching, Hungarian Bipartite Matching
Problem
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.
Â
Example 1:
Input: grid = [[1,1,1],
[1,0,1],
[0,0,1]]
Output: 3
Explanation: The invitations are sent as follows:
- The 1st boy invites the 2nd girl.
- The 2nd boy invites the 1st girl.
- The 3rd boy invites the 3rd girl.
Example 2:
Input: grid = [[1,0,1,0],
[1,0,0,0],
[0,0,1,0],
[1,1,1,0]]
Output: 3
Explanation: The invitations are sent as follows:
-The 1st boy invites the 3rd girl.
-The 2nd boy invites the 1st girl.
-The 3rd boy invites no one.
-The 4th boy invites the 2nd girl.
Â
Constraints:
grid.length == mgrid[i].length == n1 <= m, n <= 200grid[i][j]is either0or1.