当前位置:网站首页>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()
}
}
边栏推荐
- How to read the resources files in the directory path?
- sudo 权限控制,简易
- 0.1 前言
- C# 构造函数业务场景测试项目
- There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
- STM8S105K4T6------串口发送和接收
- 小程序+新零售,玩转行业新玩法!
- Asynchronous programming solution Generator generator function, iterator iterator, async/await, Promise
- 单片机C语言->的用法,和意思
- The browser
猜你喜欢
织梦内核电动伸缩门卷闸门门业公司网站模板 带手机版【站长亲测】
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
In a more general sense, calculating the displacement distance and assumptions
全网没有之一的JMeter 接口测试流程详解
v-model
cdh6.x 集成spark-sql
2022年茶艺师(中级)考试试题模拟考试平台操作
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
Small Turtle Compilation Notes
Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
随机推荐
Web APIs BOM - operating browser: swiper plug-in
DIY电工维修如何拆卸和安装开关面板插座
2022年T电梯修理考题及答案
关联接口测试
Utilities of Ruineng Micrometer Chip RN2026
Example 035: Setting the output color
DHCP服务详解
2022G1工业锅炉司炉考试练习题及模拟考试
2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试
SAP SD module foreground operation
In a more general sense, calculating the displacement distance and assumptions
How to read the resources files in the directory path?
融云「音视频架构实践」技术专场【内含完整PPT】
View mysql deadlock syntax
如何读取 resources 目录下的文件路径?
STM32-遥感数据处理
TOML配置文件格式,YAML最有力的竞争者
C程序编译和预定义详解
C program compilation and predefined detailed explanation
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比