diff --git a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/README.md b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/README.md index e8b23d050..b36a6940b 100755 --- a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/README.md +++ b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/README.md @@ -1,28 +1,46 @@ # [2285.Maximum Total Importance of Roads][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 +You are given an integer `n` denoting the number of cities in a country. The cities are numbered from `0` to `n - 1`. + +You are also given a 2D integer array `roads` where `roads[i] = [ai, bi]` denotes that there exists a **bidirectional** road connecting cities `ai` and `bi`. + +You need to assign each city with an integer value from `1` to `n`, where each value can only be used **once**. The **importance** of a road is then defined as the **sum** of the values of the two cities it connects. + +Return the **maximum total importance** of all roads possible after assigning the values optimally. -**Example 1:** +**Example 1:** + +![1](./ex1drawio.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]] +Output: 43 +Explanation: The figure above shows the country and the assigned values of [2,4,5,3,1]. +- The road (0,1) has an importance of 2 + 4 = 6. +- The road (1,2) has an importance of 4 + 5 = 9. +- The road (2,3) has an importance of 5 + 3 = 8. +- The road (0,2) has an importance of 2 + 5 = 7. +- The road (1,3) has an importance of 4 + 3 = 7. +- The road (2,4) has an importance of 5 + 1 = 6. +The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43. +It can be shown that we cannot obtain a greater total importance than 43. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./ex2drawio.png) -### 思路1 -> ... -Maximum Total Importance of Roads -```go ``` - +Input: n = 5, roads = [[0,3],[2,4],[1,3]] +Output: 20 +Explanation: The figure above shows the country and the assigned values of [4,3,2,5,1]. +- The road (0,3) has an importance of 4 + 5 = 9. +- The road (2,4) has an importance of 2 + 1 = 3. +- The road (1,3) has an importance of 3 + 5 = 8. +The total importance of all roads is 9 + 3 + 8 = 20. +It can be shown that we cannot obtain a greater total importance than 20. +``` ## 结语 diff --git a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution.go b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution.go index d115ccf5e..e4b3b148f 100644 --- a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution.go +++ b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution.go @@ -1,5 +1,21 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(n int, roads [][]int) int64 { + ans := int64(0) + + // 检查入读和出度 + in := make([]int, n) + for _, r := range roads { + in[r[0]]++ + in[r[1]]++ + } + sort.Ints(in) + v := int64(1) + for _, i := range in { + ans += v * int64(i) + v++ + } + return ans } diff --git a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution_test.go b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution_test.go index 14ff50eb4..736baf452 100644 --- a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution_test.go +++ b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n int + roads [][]int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, [][]int{{0, 1}, {1, 2}, {2, 3}, {0, 2}, {1, 3}, {2, 4}}, 43}, + {"TestCase2", 5, [][]int{{0, 3}, {2, 4}, {1, 3}}, 20}, } // 开始测试 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() { } diff --git a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/ex1drawio.png b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/ex1drawio.png new file mode 100644 index 000000000..3dc7f3e90 Binary files /dev/null and b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/ex1drawio.png differ diff --git a/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/ex2drawio.png b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/ex2drawio.png new file mode 100644 index 000000000..35e168939 Binary files /dev/null and b/leetcode/2201-2300/2285.Maximum-Total-Importance-of-Roads/ex2drawio.png differ