-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1615.maximal-network-rank.py
60 lines (50 loc) · 2 KB
/
1615.maximal-network-rank.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
# @lc app=leetcode id=1615 lang=python3
#
# [1615] Maximal Network Rank
#
# @lc code=start
# TAGS: Graph
class Solution:
# 388 ms, 51.48%. Time: O(N^2). Space: O(N)
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
if not roads: return 0
graph = collections.defaultdict(list)
counter = [0] * n
for frm, to in roads:
counter[frm] += 1
counter[to] += 1
graph[frm].append(to)
graph[to].append(frm)
max_network_rank = 0
for cnt1 in range(n):
for cnt2 in range(cnt1 + 1, n):
network_rank = self.get_network_rank(cnt1, cnt2, graph, counter)
max_network_rank = max(max_network_rank, network_rank)
return max_network_rank
def get_network_rank(self, cnt1, cnt2, graph, counter):
overlap = cnt1 in graph[cnt2]
return counter[cnt1] + counter[cnt2] - overlap
# 300 ms, 97.54%. Time: O(N^2). Space: O(N). Same complexity but slightly more optimized.
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
if not roads: return 0
graph = collections.defaultdict(list)
counter = [0] * n
for frm, to in roads:
counter[frm] += 1
counter[to] += 1
graph[frm].append(to)
graph[to].append(frm)
max_counter = max(counter)
second_max_counter = 0
for cnt in counter:
if cnt != max_counter:
second_max_counter = max(second_max_counter, cnt)
new_counter = [i for i, cnt in enumerate(counter) if cnt in (max_counter, second_max_counter)]
max_network_rank = 0
for i, cnt1 in enumerate(new_counter):
for cnt2 in new_counter[i + 1:]:
overlap = cnt1 in graph[cnt2]
max_network_rank = max(max_network_rank, counter[cnt1] + counter[cnt2] - overlap)
return max_network_rank
# @lc code=end