Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1820: Maximum Number of Accepted Invitations

In this guide, we solve Leetcode #1820 Maximum Number of Accepted Invitations in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Depth-First Search, Graph, Array, Matrix

Intuition

The data forms a graph, so we should explore nodes and edges systematically.

A traversal ensures we visit each node once while maintaining the needed state.

Approach

Build an adjacency list and traverse with BFS or DFS.

Aggregate results as you visit nodes.

Steps:

  • Build the graph.
  • Traverse with BFS/DFS.
  • Accumulate the required output.

Example

Input: grid = [[1,1,1], [1,0,1], [0,0,1]] Output: 3 Explanation: The invitations are sent as follows: - The 1st boy invites the 2nd girl. - The 2nd boy invites the 1st girl. - The 3rd boy invites the 3rd girl.

Python Solution

class Solution: def maximumInvitations(self, grid: List[List[int]]) -> int: def find(i): for j, v in enumerate(grid[i]): if v and j not in vis: vis.add(j) if match[j] == -1 or find(match[j]): match[j] = i return True return False m, n = len(grid), len(grid[0]) match = [-1] * n ans = 0 for i in range(m): vis = set() ans += find(i) return ans

Complexity

The time complexity is O(m×n)O(m \times n)O(m×n). The space complexity is O(V).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy