MediumArrayDepth-First SearchGraphMatrix

Maximum Number of Accepted Invitations

LeetCode
1 approach, code in all languages

There are m boys and n girls at a party, described by an m x n binary matrix grid. If grid[i][j] == 1, then boy i is allowed to invite girl j to dance; otherwise he cannot.

Each boy may invite at most one girl, and each girl may accept at most one invitation. You want to arrange the invitations so that as many dance pairs form as possible.

Return the maximum possible number of accepted invitations.

Example 1

Input: grid = [[1,1,1],[1,0,1],[0,0,1]]

Output: 3

Boy 0 invites girl 1, boy 1 invites girl 0, and boy 2 invites girl 2. All three invitations are accepted.

Example 2

Input: grid = [[1,0,1,0],[1,0,0,0],[0,0,1,0],[1,1,1,0]]

Output: 3

One optimal pairing is boy 0 -> girl 2, boy 1 -> girl 0, and boy 3 -> girl 1. Boy 2 shares girl 2 with boy 0, so only three matches are possible.

Constraints

  • grid.length == m
  • grid[i].length == n
  • 1 <= m, n <= 200
  • grid[i][j] is either 0 or 1
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now