返回介绍

solution / 1600-1699 / 1615.Maximal Network Rank / README_EN

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

1615. Maximal Network Rank

中文文档

Description

There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi.

The network rank_ _of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once.

The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities.

Given the integer n and the array roads, return _the maximal network rank of the entire infrastructure_.

 

Example 1:

Input: n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
Output: 4
Explanation: The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.

Example 2:

Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
Output: 5
Explanation: There are 5 roads that are connected to cities 1 or 2.

Example 3:

Input: n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
Output: 5
Explanation: The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.

 

Constraints:

  • 2 <= n <= 100
  • 0 <= roads.length <= n * (n - 1) / 2
  • roads[i].length == 2
  • 0 <= ai, bi <= n-1
  • ai != bi
  • Each pair of cities has at most one road connecting them.

Solutions

Solution 1

class Solution:
  def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
    g = defaultdict(set)
    for a, b in roads:
      g[a].add(b)
      g[b].add(a)
    ans = 0
    for a in range(n):
      for b in range(a + 1, n):
        if (t := len(g[a]) + len(g[b]) - (a in g[b])) > ans:
          ans = t
    return ans
class Solution {
  public int maximalNetworkRank(int n, int[][] roads) {
    int[][] g = new int[n][n];
    int[] cnt = new int[n];
    for (var r : roads) {
      int a = r[0], b = r[1];
      g[a][b] = 1;
      g[b][a] = 1;
      ++cnt[a];
      ++cnt[b];
    }
    int ans = 0;
    for (int a = 0; a < n; ++a) {
      for (int b = a + 1; b < n; ++b) {
        ans = Math.max(ans, cnt[a] + cnt[b] - g[a][b]);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int maximalNetworkRank(int n, vector<vector<int>>& roads) {
    int cnt[n];
    int g[n][n];
    memset(cnt, 0, sizeof(cnt));
    memset(g, 0, sizeof(g));
    for (auto& r : roads) {
      int a = r[0], b = r[1];
      g[a][b] = g[b][a] = 1;
      ++cnt[a];
      ++cnt[b];
    }
    int ans = 0;
    for (int a = 0; a < n; ++a) {
      for (int b = a + 1; b < n; ++b) {
        ans = max(ans, cnt[a] + cnt[b] - g[a][b]);
      }
    }
    return ans;
  }
};
func maximalNetworkRank(n int, roads [][]int) (ans int) {
  g := make([][]int, n)
  cnt := make([]int, n)
  for i := range g {
    g[i] = make([]int, n)
  }
  for _, r := range roads {
    a, b := r[0], r[1]
    g[a][b], g[b][a] = 1, 1
    cnt[a]++
    cnt[b]++
  }
  for a := 0; a < n; a++ {
    for b := a + 1; b < n; b++ {
      ans = max(ans, cnt[a]+cnt[b]-g[a][b])
    }
  }
  return
}
function maximalNetworkRank(n: number, roads: number[][]): number {
  const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
  const cnt: number[] = new Array(n).fill(0);
  for (const [a, b] of roads) {
    g[a][b] = 1;
    g[b][a] = 1;
    ++cnt[a];
    ++cnt[b];
  }
  let ans = 0;
  for (let a = 0; a < n; ++a) {
    for (let b = a + 1; b < n; ++b) {
      ans = Math.max(ans, cnt[a] + cnt[b] - g[a][b]);
    }
  }
  return ans;
}

Solution 2

class Solution:
  def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
    g = [[0] * n for _ in range(n)]
    cnt = [0] * n
    for a, b in roads:
      g[a][b] = g[b][a] = 1
      cnt[a] += 1
      cnt[b] += 1
    return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))

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

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

发布评论

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