Skip to content

Commit b89c985

Browse files
committed
1615. Maximal Network Rank: AC
1 parent 7d2463d commit b89c985

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1217,3 +1217,4 @@ mod s1609_even_odd_tree;
12171217
mod s1610_maximum_number_of_visible_points;
12181218
mod s1611_minimum_one_bit_operations_to_make_integers_zero;
12191219
mod s1614_maximum_nesting_depth_of_the_parentheses;
1220+
mod s1615_maximal_network_rank;
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* [1615] Maximal Network Rank
3+
*
4+
* 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.
5+
* 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.
6+
* The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities.
7+
* Given the integer n and the array roads, return the maximal network rank of the entire infrastructure.
8+
*
9+
* Example 1:
10+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/ex1.png" style="width: 292px; height: 172px;" />
11+
*
12+
* Input: n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
13+
* Output: 4
14+
* 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.
15+
*
16+
* Example 2:
17+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/ex2.png" style="width: 292px; height: 172px;" />
18+
*
19+
* Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
20+
* Output: 5
21+
* Explanation: There are 5 roads that are connected to cities 1 or 2.
22+
*
23+
* Example 3:
24+
*
25+
* Input: n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
26+
* Output: 5
27+
* Explanation: The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.
28+
*
29+
*
30+
* Constraints:
31+
*
32+
* 2 <= n <= 100
33+
* 0 <= roads.length <= n * (n - 1) / 2
34+
* roads[i].length == 2
35+
* 0 <= ai, bi <= n-1
36+
* ai != bi
37+
* Each pair of cities has at most one road connecting them.
38+
*
39+
*/
40+
pub struct Solution {}
41+
42+
// problem: https://leetcode.com/problems/maximal-network-rank/
43+
// discuss: https://leetcode.com/problems/maximal-network-rank/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
impl Solution {
48+
pub fn maximal_network_rank(n: i32, roads: Vec<Vec<i32>>) -> i32 {
49+
let n = n as usize;
50+
let mut degree = vec![std::collections::HashSet::new(); n];
51+
for v in roads {
52+
degree[v[0] as usize].insert(v[1] as usize);
53+
degree[v[1] as usize].insert(v[0] as usize);
54+
}
55+
56+
let mut result = 0;
57+
58+
for i in 0..n - 1 {
59+
for j in i + 1..n {
60+
let mut tmp = degree[i].len() + degree[j].len();
61+
if degree[i].contains(&j) {
62+
tmp -= 1;
63+
}
64+
result = result.max(tmp);
65+
}
66+
}
67+
68+
result as i32
69+
}
70+
}
71+
72+
// submission codes end
73+
74+
#[cfg(test)]
75+
mod tests {
76+
use super::*;
77+
78+
#[test]
79+
fn test_1615_example_1() {
80+
let n = 4;
81+
let roads = vec![vec![0, 1], vec![0, 3], vec![1, 2], vec![1, 3]];
82+
83+
let result = 4;
84+
85+
assert_eq!(Solution::maximal_network_rank(n, roads), result);
86+
}
87+
88+
#[test]
89+
fn test_1615_example_2() {
90+
let n = 5;
91+
let roads = vec![
92+
vec![0, 1],
93+
vec![0, 3],
94+
vec![1, 2],
95+
vec![1, 3],
96+
vec![2, 3],
97+
vec![2, 4],
98+
];
99+
100+
let result = 5;
101+
102+
assert_eq!(Solution::maximal_network_rank(n, roads), result);
103+
}
104+
105+
fn test_1615_example_3() {
106+
let n = 8;
107+
let roads = vec![
108+
vec![0, 1],
109+
vec![1, 2],
110+
vec![2, 3],
111+
vec![2, 4],
112+
vec![5, 6],
113+
vec![5, 7],
114+
];
115+
116+
let result = 5;
117+
118+
assert_eq!(Solution::maximal_network_rank(n, roads), result);
119+
}
120+
}

0 commit comments

Comments
 (0)