[LeetCode] 1615. Maximal Network Rank


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.

最大網絡秩。

n 座城市和一些連接這些城市的道路 roads 共同組成一個基礎設施網絡。每個 roads[i] = [ai, bi] 都表示在城市 ai 和 bi 之間有一條雙向道路。

兩座不同城市構成的 城市對 的 網絡秩 定義為:與這兩座城市 直接 相連的道路總數。如果存在一條道路直接連接這兩座城市,則這條道路只計算 一次 。

整個基礎設施網絡的 最大網絡秩 是所有不同城市對中的 最大網絡秩 。

給你整數 n 和數組 roads,返回整個基礎設施網絡的 最大網絡秩 。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/maximal-network-rank
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

題意如上,本質上是在找input里兩點之間最大的網絡秩rank。這是一道圖論的題,注意每條給出的邊都是無向的所以對於一條邊上的兩個點來說,他們的rank都各自 + 1。思路是首先用一個數組edges記錄每個點各自的rank是多少,每遇到一條邊,這條邊的兩端的點的rank都各自 + 1;同時我們需要一個二維的鄰接矩陣adj,這里我選擇用boolean,因為我們只需要記錄兩點之間是否是鄰接點即可。

我們遍歷一遍roads數組之后,我們可以把edges數組和adj矩陣都統計完畢。之后我們再用兩個for loop遍歷這N個點,看看每兩個不同的點之間的rank sum。注意如果兩點是能直接相連的,他們在鄰接矩陣adj里就會是true,此時rank需要減一。

時間O(n)

空間O(n)

Java實現

 1 class Solution {
 2     public int maximalNetworkRank(int n, int[][] roads) {
 3         // corner case
 4         if (roads == null || roads.length == 0) {
 5             return 0;
 6         }
 7 
 8         // normal case
 9         int res = Integer.MIN_VALUE;
10         int rank = Integer.MIN_VALUE;
11         int[] edges = new int[n + 1];
12         boolean[][] adj = new boolean[n + 1][n + 1];
13         for (int i = 0; i < roads.length; i++) {
14             int from = roads[i][0];
15             int to = roads[i][1];
16             edges[from]++;
17             edges[to]++;
18             adj[from][to] = true;
19             adj[to][from] = true;
20         }
21 
22         for (int i = 0; i < n; i++) {
23             for (int j = i + 1; j < n; j++) {
24                 rank = edges[i] + edges[j] - (adj[i][j] == true ? 1 : 0);
25                 res = Math.max(res, rank);
26             }
27         }
28         return res;
29     }
30 }

 

LeetCode 題目總結


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM