当前位置:网站首页>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()
}
}
边栏推荐
- 云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
- 香港服务器有哪些常用的型号
- 安装postgis时报找不到“POSTGIS_VERSION”这个函数
- 【指针内功修炼】深度剖析指针笔试题(三)
- Deep Learning (3) Classification Theory Part
- Mini program + new retail, play the new way of playing in the industry!
- Example 041: Methods and variables of a class
- 小程序:扫码打开参数解析
- golang中的unsafe.Pointer,指针,引用
- Example 037: Sorting
猜你喜欢

Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products

mpf5_定价Bond_yield curve_Spot coupon_duration_有效利率_连续复利_远期_Vasicek短期_CIR模型Derivatives_Tridiagonal_ppf

倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即

STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目

Engineering drawing review questions (with answers)

从图文展示到以云为核,第五代验证码独有的策略情报能力

In a more general sense, calculating the displacement distance and assumptions

uni-app 从零开始-基础模版(一)

esp8266-01s刷固件步骤

cdh6.x 集成spark-sql
随机推荐
一文看懂推荐系统:召回04:离散特征处理,one-hot编码和embedding特征嵌入
C程序编译和预定义详解
2022焊工(初级)上岗证题目模拟考试平台操作
香港服务器有哪些常用的型号
验证码业务逻辑漏洞
Web APIs BOM - operating browser: swiper plug-in
Utilities of Ruineng Micrometer Chip RN2026
Parquet encoding
第13章 网络安全漏洞防护技术原理与应用
mpf5_定价Bond_yield curve_Spot coupon_duration_有效利率_连续复利_远期_Vasicek短期_CIR模型Derivatives_Tridiagonal_ppf
html select tag assignment database query result
yum 仅下载包
Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
如何在MySQL中的数据库下删除所有的表
Day13 Postman的使用
STM8S-----option byte
unsafe.Pointer, pointer, reference in golang
golang中的unsafe.Pointer,指针,引用
v-model
Why use Selenium for automated testing