Skip to content

Commit 9219ac4

Browse files
authored
Merge pull request #597 from 0xff-dev/1615
Add solution and test-cases for problem 1615
2 parents cb950b4 + 00ffc6b commit 9219ac4

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

leetcode/1601-1700/1615.Maximal-Network-Rank/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1615.Maximal Network Rank][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
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 a<sub>i</sub> and b<sub>i</sub>.
5+
6+
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**.
7+
8+
The **maximal network rank** of the infrastructure is the **maximum network rank** of all pairs of different cities.
9+
10+
Given the integer `n` and the array `roads`, return the **maximal network rank of the entire infrastructure.
11+
12+
**Example 1:**
713

8-
**Example 1:**
14+
![example1](./ex1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
18+
Output: 4
19+
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.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
24+
![example2](./ex2.png)
1925

20-
### 思路1
21-
> ...
22-
Maximal Network Rank
23-
```go
2426
```
27+
Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
28+
Output: 5
29+
Explanation: There are 5 roads that are connected to cities 1 or 2.
30+
```
31+
32+
**Example 3:**
2533

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

2740
## 结语
2841

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, roads [][]int) int {
4+
adj := make([][]bool, n)
5+
for i := 0; i < n; i++ {
6+
adj[i] = make([]bool, n)
7+
}
8+
count := make([]int, n)
9+
for _, road := range roads {
10+
from, to := road[0], road[1]
11+
adj[from][to] = true
12+
adj[to][from] = true
13+
count[from]++
14+
count[to]++
15+
}
16+
ans := 0
17+
18+
for c1 := 0; c1 < n-1; c1++ {
19+
for c2 := c1 + 1; c2 < n; c2++ {
20+
r := count[c1] + count[c2]
21+
if adj[c1][c2] {
22+
r--
23+
}
24+
if r > ans {
25+
ans = r
26+
}
27+
}
28+
}
29+
30+
return ans
531
}

leetcode/1601-1700/1615.Maximal-Network-Rank/Solution_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
roads [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, [][]int{
18+
{0, 1}, {0, 3}, {1, 2}, {1, 3},
19+
}, 4},
20+
{"TestCase2", 5, [][]int{
21+
{0, 1}, {0, 3}, {1, 2}, {1, 3}, {2, 3}, {2, 4},
22+
}, 5},
23+
{"TestCase3", 8, [][]int{
24+
{0, 1}, {1, 2}, {2, 3}, {2, 4}, {5, 6}, {5, 7},
25+
}, 5},
1926
}
2027

2128
// 开始测试
2229
for i, c := range cases {
2330
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
31+
got := Solution(c.n, c.roads)
2532
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
34+
c.expect, got, c.n, c.roads)
2835
}
2936
})
3037
}
3138
}
3239

33-
//压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
//使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}
9.53 KB
Loading
10 KB
Loading

0 commit comments

Comments
 (0)