Skip to content

Commit 68cdfdb

Browse files
committed
1 parent cf71e7a commit 68cdfdb

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,3 +1731,4 @@ mod s2280_minimum_lines_to_represent_a_line_chart;
17311731
mod s2281_sum_of_total_strength_of_wizards;
17321732
mod s2283_check_if_number_has_equal_digit_count_and_digit_value;
17331733
mod s2284_sender_with_largest_word_count;
1734+
mod s2285_maximum_total_importance_of_roads;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* [2285] Maximum Total Importance of Roads
3+
*
4+
* You are given an integer n denoting the number of cities in a country. The cities are numbered from 0 to n - 1.
5+
* 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.
6+
* 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.
7+
* Return the maximum total importance of all roads possible after assigning the values optimally.
8+
*
9+
* Example 1:
10+
* <img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png" style="width: 290px; height: 215px;" />
11+
* Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
12+
* Output: 43
13+
* Explanation: The figure above shows the country and the assigned values of [2,4,5,3,1].
14+
* - The road (0,1) has an importance of 2 + 4 = 6.
15+
* - The road (1,2) has an importance of 4 + 5 = 9.
16+
* - The road (2,3) has an importance of 5 + 3 = 8.
17+
* - The road (0,2) has an importance of 2 + 5 = 7.
18+
* - The road (1,3) has an importance of 4 + 3 = 7.
19+
* - The road (2,4) has an importance of 5 + 1 = 6.
20+
* The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
21+
* It can be shown that we cannot obtain a greater total importance than 43.
22+
*
23+
* Example 2:
24+
* <img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png" style="width: 281px; height: 151px;" />
25+
* Input: n = 5, roads = [[0,3],[2,4],[1,3]]
26+
* Output: 20
27+
* Explanation: The figure above shows the country and the assigned values of [4,3,2,5,1].
28+
* - The road (0,3) has an importance of 4 + 5 = 9.
29+
* - The road (2,4) has an importance of 2 + 1 = 3.
30+
* - The road (1,3) has an importance of 3 + 5 = 8.
31+
* The total importance of all roads is 9 + 3 + 8 = 20.
32+
* It can be shown that we cannot obtain a greater total importance than 20.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 2 <= n <= 5 * 10^4
38+
* 1 <= roads.length <= 5 * 10^4
39+
* roads[i].length == 2
40+
* 0 <= ai, bi <= n - 1
41+
* ai != bi
42+
* There are no duplicate roads.
43+
*
44+
*/
45+
pub struct Solution {}
46+
47+
// problem: https://leetcode.com/problems/maximum-total-importance-of-roads/
48+
// discuss: https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/?currentPage=1&orderBy=most_votes&query=
49+
50+
// submission codes start here
51+
52+
impl Solution {
53+
// Credit: https://leetcode.com/problems/maximum-total-importance-of-roads/solutions/5380938/rust-32ms-555mb-one-liner-solution-by-ro-xpyj/
54+
pub fn maximum_importance(n: i32, roads: Vec<Vec<i32>>) -> i64 {
55+
roads
56+
.into_iter()
57+
.fold(vec![0; n as usize], |mut v, x| {
58+
v[x[0] as usize] += 1;
59+
v[x[1] as usize] += 1;
60+
v
61+
})
62+
.into_iter()
63+
.collect::<std::collections::BinaryHeap<i32>>()
64+
.into_sorted_vec()
65+
.into_iter()
66+
.enumerate()
67+
.map(|(i, x)| (i + 1) as i64 * x as i64)
68+
.sum::<i64>()
69+
}
70+
}
71+
72+
// submission codes end
73+
74+
#[cfg(test)]
75+
mod tests {
76+
use super::*;
77+
78+
#[test]
79+
fn test_2285_example_1() {
80+
let n = 5;
81+
let roads = vec![
82+
vec![0, 1],
83+
vec![1, 2],
84+
vec![2, 3],
85+
vec![0, 2],
86+
vec![1, 3],
87+
vec![2, 4],
88+
];
89+
90+
let result = 43;
91+
92+
assert_eq!(Solution::maximum_importance(n, roads), result);
93+
}
94+
95+
#[test]
96+
fn test_2285_example_2() {
97+
let n = 5;
98+
let roads = vec![vec![0, 3], vec![2, 4], vec![1, 3]];
99+
100+
let result = 20;
101+
102+
assert_eq!(Solution::maximum_importance(n, roads), result);
103+
}
104+
}

0 commit comments

Comments
 (0)