Maximum Number of Accepted Invitations - 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 i-th boy can invite the j-th 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.
Input & Output
Example 1 — Basic Party Invitations
$
Input:
grid = [[1,1,1],[1,0,1],[0,0,1]]
›
Output:
2
💡 Note:
Boy 0 can invite any of the 3 girls. Boy 1 can invite girls 0 or 2. Boy 2 can only invite girl 2. Optimal matching: Boy 0→Girl 1, Boy 1→Girl 0, but Boy 2 can only get Girl 2 which conflicts with Boy 1. So optimal is Boy 0→Girl 0, Boy 1→Girl 2, giving 2 total matches.
Example 2 — Limited Options
$
Input:
grid = [[1,0,0,0],[1,0,0,0],[1,1,1,1]]
›
Output:
2
💡 Note:
Boys 0 and 1 can only invite Girl 0, so only one of them can succeed. Boy 2 can invite any girl (0,1,2,3). Optimal matching: Boy 0→Girl 0, Boy 2→Girl 1, giving 2 total matches.
Example 3 — No Matches Possible
$
Input:
grid = [[0]]
›
Output:
0
💡 Note:
The only boy cannot invite the only girl (grid[0][0] = 0), so no invitations are accepted.
Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m, n ≤ 200
- grid[i][j] is either 0 or 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code