Maximum Number of Accepted Invitations - Problem
Party Invitation Matching Problem

Imagine you're organizing a school party where m boys want to invite n girls as their dance partners. You have an m ร— n binary matrix grid where:

โ€ข grid[i][j] = 1 means the i-th boy can invite the j-th girl
โ€ข grid[i][j] = 0 means they're incompatible

Rules:
๐ŸŽฏ Each boy can invite at most one girl
๐ŸŽฏ Each girl can accept at most one invitation

Your goal is to find the maximum number of successful boy-girl pairs that can be formed for the party. This is a classic bipartite matching problem where we need to optimally pair elements from two distinct sets.

Input & Output

example_1.py โ€” Basic matching
$ 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, and Boy 2 can invite Girl 2. All three invitations can be accepted simultaneously, resulting in 3 successful pairs.
example_2.py โ€” Limited compatibility
$ Input: grid = [[1,0,0,0],[1,0,0,0],[0,0,0,0]]
โ€บ Output: 1
๐Ÿ’ก Note: Only Girl 0 is compatible with both Boy 0 and Boy 1, but she can accept only one invitation. Boy 2 has no compatible girls. Maximum matches = 1.
example_3.py โ€” Perfect matching
$ Input: grid = [[1,1],[1,1]]
โ€บ Output: 2
๐Ÿ’ก Note: Both boys are compatible with both girls. We can form pairs (Boy 0, Girl 0) and (Boy 1, Girl 1), or (Boy 0, Girl 1) and (Boy 1, Girl 0). Either way, maximum matches = 2.

Visualization

Tap to expand
๐ŸŽ‰ Party Invitation Matching Problem๐Ÿ‘ฆ BoysB0B1B2๐Ÿ‘ง GirlsG0G1G2โœ“ Successful Matchโ‹ฏ Possible ConnectionAlgorithm Steps:1. For each boy, use DFS to find available girl2. If girl is taken, try to reassign her current partner3. Create augmenting paths to maximize matches4. Count total successful boy-girl pairs formed
Understanding the Visualization
1
Build Compatibility Graph
Create a bipartite graph where edges represent possible boy-girl pairs
2
Apply Matching Algorithm
Use DFS to find augmenting paths that maximize total matches
3
Handle Conflicts
When a girl is already matched, try to reassign her current partner
4
Count Maximum Pairs
Return the total number of successful boy-girl pairs formed
Key Takeaway
๐ŸŽฏ Key Insight: The optimal solution uses DFS to find augmenting paths in the bipartite graph, allowing reassignment of existing matches to accommodate new ones, thus maximizing the total number of successful pairings.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m ร— n ร— (m + n))

For each boy, we may need to explore all edges in worst case

n
2n
โœ“ Linear Growth
Space Complexity
O(n + m)

Space for match arrays and visited tracking during DFS

n
2n
โšก Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 โ‰ค m, n โ‰ค 200
  • grid[i][j] is either 0 or 1
  • Each boy can invite at most one girl
  • Each girl can accept at most one invitation
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
42.0K Views
Medium-High Frequency
~25 min Avg. Time
1.4K 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