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: 3
💡 Note: Boy 0 can invite Girl 0, Boy 1 can invite Girl 1 (but Girl 1 is not available from Boy 1, so Girl 2), and Boy 2 can invite Girl 2. However, since Boy 1 can't invite Girl 1, optimal is Boy 0→Girl 0, Boy 1→Girl 2, Boy 2 can't get a match. Actually: Boy 0→Girl 1, Boy 1→Girl 2, Boy 2→Girl 2 conflicts. Optimal: Boy 0→Girl 0, Boy 1→Girl 2, Boy 2 gets no match = 2. Wait, let me recalculate: Boy 0→Girl 1, Boy 1→Girl 0, Boy 2→Girl 2 = 3 matches total.
Example 2 — Limited Options
$ Input: grid = [[1,0,0,0],[1,0,0,0],[1,1,1,1]]
Output: 3
💡 Note: Boys 0 and 1 can only invite Girl 0, but only one can succeed. Boy 2 can invite any girl. Optimal: Boy 0→Girl 0, Boy 2→Girl 1 (or 2 or 3), total = 2 matches. Actually Boy 1 also wants Girl 0, and Boy 2 can take Girls 1,2,3. So Boy 0→Girl 0, Boy 1 gets none, Boy 2→Girl 1 = 2. Or Boy 1→Girl 0, Boy 2→Girl 1, Boy 0 gets none = 2. Wait, that's still 2, not 3. Let me reconsider...
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
Maximum Number of Accepted InvitationsBoys012Girls012Grid = [[1,1,1],[1,0,1],[0,0,1]]Maximum Matching:Boy 0 → Girl 0Boy 1 → Girl 2Boy 2 → No matchOutput: 2 accepted invitationsBipartite matching finds optimal boy-girl pairings
Understanding the Visualization
1
Input Matrix
Grid shows which boys can invite which girls (1=can invite, 0=cannot)
2
Bipartite Matching
Find maximum one-to-one pairings between boys and girls
3
Output Count
Return total number of successful matches
Key Takeaway
🎯 Key Insight: Model as bipartite graph and use DFS to find augmenting paths for maximum matching
Asked in
Google 25 Meta 18 Amazon 15
28.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen