-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmaximal-network-rank.py
More file actions
29 lines (22 loc) · 875 Bytes
/
maximal-network-rank.py
File metadata and controls
29 lines (22 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import List, Set, Tuple
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
degrees: List[int] = [0] * n
has_edge: Set[Tuple[int, int]] = set()
for src, dst in roads:
degrees[src] += 1
degrees[dst] += 1
has_edge.add((src, dst))
has_edge.add((dst, src))
max_network_rank = 0
# O(n*n) time complexity. The solution is not optimal, O(n + m) is
# possible and quite obvious, but I'm too lazy today
for src in range(n):
for dst in range(n):
if src == dst:
continue
max_network_rank = max(
max_network_rank,
degrees[src] + degrees[dst] - int((src, dst) in has_edge),
)
return max_network_rank