Skip to content

Commit

Permalink
Merge pull request #597 from 0xff-dev/1615
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1615
  • Loading branch information
6boris authored Aug 22, 2023
2 parents cb950b4 + 00ffc6b commit 9219ac4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
39 changes: 26 additions & 13 deletions leetcode/1601-1700/1615.Maximal-Network-Rank/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [1615.Maximal Network Rank][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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>.

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:**

**Example 1:**
![example1](./ex1.png)

```
Input: a = "11", b = "1"
Output: "100"
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:**

## 题解
![example2](./ex2.png)

### 思路1
> ...
Maximal Network Rank
```go
```
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.
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/1601-1700/1615.Maximal-Network-Rank/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int, roads [][]int) int {
adj := make([][]bool, n)
for i := 0; i < n; i++ {
adj[i] = make([]bool, n)
}
count := make([]int, n)
for _, road := range roads {
from, to := road[0], road[1]
adj[from][to] = true
adj[to][from] = true
count[from]++
count[to]++
}
ans := 0

for c1 := 0; c1 < n-1; c1++ {
for c2 := c1 + 1; c2 < n; c2++ {
r := count[c1] + count[c2]
if adj[c1][c2] {
r--
}
if r > ans {
ans = r
}
}
}

return ans
}
27 changes: 17 additions & 10 deletions leetcode/1601-1700/1615.Maximal-Network-Rank/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,37 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n int
roads [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, [][]int{
{0, 1}, {0, 3}, {1, 2}, {1, 3},
}, 4},
{"TestCase2", 5, [][]int{
{0, 1}, {0, 3}, {1, 2}, {1, 3}, {2, 3}, {2, 4},
}, 5},
{"TestCase3", 8, [][]int{
{0, 1}, {1, 2}, {2, 3}, {2, 4}, {5, 6}, {5, 7},
}, 5},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.roads)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.roads)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9219ac4

Please sign in to comment.