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
Maximum Number of Accepted Invitations INPUT m x n Grid (Boys x Girls) G0 G1 G2 B0 1 1 1 B1 1 0 1 B2 0 0 1 = Can invite = Cannot Bipartite Graph View B0 B1 B2 G0 G1 G2 ALGORITHM STEPS 1 Initialize Matching Girls matched = [-1,-1,-1] 2 DFS for Each Boy Find augmenting path 3 Try Each Girl If free or can reassign 4 Count Matches Sum successful paths Augmenting Path Search B0 G0 OK B1 G2 OK B2 Reassign! FINAL RESULT Maximum Matching Found B0 G1 B1 G0 B2 G2 Output: 3 All 3 boys matched! match = [G1, G0, G2] 3 accepted invitations Key Insight: This is a Maximum Bipartite Matching problem. DFS with augmenting paths (Hungarian method) finds the optimal matching by allowing reassignment of existing pairs when a new boy needs a match. Time Complexity: O(m * n * m) where m=boys, n=girls. Each boy triggers DFS visiting all edges. TutorialsPoint - Maximum Number of Accepted Invitations | DFS with Augmenting Paths
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