返回介绍

solution / 1800-1899 / 1820.Maximum Number of Accepted Invitations / README_EN

发布于 2024-06-17 01:03:14 字数 5369 浏览 0 评论 0 收藏 0

1820. Maximum Number of Accepted Invitations

中文文档

Description

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 ith boy can invite the jth 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._

 

Example 1:

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.

Example 2:

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

 

Constraints:

  • grid.length == m
  • grid[i].length == n
  • 1 <= m, n <= 200
  • grid[i][j] is either 0 or 1.

Solutions

Solution 1: Hungarian Algorithm

This problem belongs to the maximum matching problem of bipartite graphs, which is suitable for solving with the Hungarian algorithm.

The core idea of the Hungarian algorithm is to continuously start from unmatched points, look for augmenting paths, and stop when there are no augmenting paths. This gives the maximum match.

The time complexity is $O(m \times n)$.

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
class Solution {
  private int[][] grid;
  private boolean[] vis;
  private int[] match;
  private int n;

  public int maximumInvitations(int[][] grid) {
    int m = grid.length;
    n = grid[0].length;
    this.grid = grid;
    vis = new boolean[n];
    match = new int[n];
    Arrays.fill(match, -1);
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      Arrays.fill(vis, false);
      if (find(i)) {
        ++ans;
      }
    }
    return ans;
  }

  private boolean find(int i) {
    for (int j = 0; j < n; ++j) {
      if (grid[i][j] == 1 && !vis[j]) {
        vis[j] = true;
        if (match[j] == -1 || find(match[j])) {
          match[j] = i;
          return true;
        }
      }
    }
    return false;
  }
}
class Solution {
public:
  int maximumInvitations(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    bool vis[210];
    int match[210];
    memset(match, -1, sizeof match);
    int ans = 0;
    function<bool(int)> find = [&](int i) -> bool {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] && !vis[j]) {
          vis[j] = true;
          if (match[j] == -1 || find(match[j])) {
            match[j] = i;
            return true;
          }
        }
      }
      return false;
    };
    for (int i = 0; i < m; ++i) {
      memset(vis, 0, sizeof vis);
      ans += find(i);
    }
    return ans;
  }
};
func maximumInvitations(grid [][]int) int {
  m, n := len(grid), len(grid[0])
  var vis map[int]bool
  match := make([]int, n)
  for i := range match {
    match[i] = -1
  }
  var find func(i int) bool
  find = func(i int) bool {
    for j, v := range grid[i] {
      if v == 1 && !vis[j] {
        vis[j] = true
        if match[j] == -1 || find(match[j]) {
          match[j] = i
          return true
        }
      }
    }
    return false
  }
  ans := 0
  for i := 0; i < m; i++ {
    vis = map[int]bool{}
    if find(i) {
      ans++
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文