当前位置:网站首页>LeetCode每日一题(2285. Maximum Total Importance of Roads)
LeetCode每日一题(2285. Maximum Total Importance of Roads)
2022-08-04 02:46:00 【wangjun861205】
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:
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:
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.
Constraints:
- 2 <= n <= 5 * 104
- 1 <= roads.length <= 5 * 104
- roads[i].length == 2
- 0 <= ai, bi <= n - 1
- ai != bi
- There are no duplicate roads.
每个节点能为最终答案贡献 m * v 的 importance, m 代表与此节点连接的路的数量, v 代表的赋予此节点的值, 这样我们不难看出, 我们应该给 m 较大的节点赋予较大的值, 所以我们只需要统计每个节点锁连接的路的数量, 然后根据路的数量排序, 然后按顺序赋值求和就可以了
impl Solution {
pub fn maximum_importance(n: i32, roads: Vec<Vec<i32>>) -> i64 {
let mut counts = vec![0; n as usize];
for road in roads {
counts[road[0] as usize] += 1;
counts[road[1] as usize] += 1;
}
counts.sort();
counts
.into_iter()
.enumerate()
.map(|(i, v)| v as i64 * (i as i64 + 1))
.sum()
}
}
边栏推荐
- 小程序+新零售,玩转行业新玩法!
- 说说数据治理中常见的20个问题
- 【学习笔记之菜Dog学C】动态内存管理
- What is the source of flinkcdc consuming mysql binlog data without sqltype=delete
- Sfdp 超级表单开发平台 V6.0.5 正式发布
- 2022.8.3-----leetcode.899
- Priority_queue element as a pointer, the overloaded operators
- SAP SD module foreground operation
- Utilities of Ruineng Micrometer Chip RN2026
- [QNX Hypervisor 2.2用户手册]10.3 vdev gic
猜你喜欢
随机推荐
Example: 036 is a prime number
TOML configuration file format, YAML's top contender
ssh服务详解
Day13 Postman的使用
cdh6.x 集成spark-sql
Web APIs BOM - operating browser: swiper plug-in
多线程间的通信方式你知道几种?
[Playwright Test Tutorial] 5 minutes to get started
简单排序(暑假每日一题 14)
2022G1工业锅炉司炉考试练习题及模拟考试
Pine Script | How to display and typeset a plot switch?
【Playwright测试教程】5分钟上手
STM8S-----option byte
Big guys, it takes a long time to read mysql3 million single tables, what parameters can be discounted, or is there any way to hurry up
Priority_queue element as a pointer, the overloaded operators
In the season of going overseas, the localization of Internet tips for going overseas
Small Turtle Compilation Notes
APP电商如何快速分润分账?
MCU C language -> usage, and meaning
flinkcdc 消费 mysql binlog 没有 sqltype=delete 的数据是什么原