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
โข
โข
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.
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 incompatibleRules:
๐ฏ 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
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
โ Linear Growth
Space Complexity
O(n + m)
Space for match arrays and visited tracking during DFS
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code