LeetCode 2497. 图中最大星和 C#
原题链接
中等
作者:
hpstory
,
2022-12-13 14:18:02
,
所有人可见
,
阅读 100
C# 代码
public class Solution {
public int MaxStarSum(int[] vals, int[][] edges, int k) {
List<List<int>> graph = new List<List<int>>();
int n = vals.Length;
for (int i = 0; i < n; i++){
graph.Add(new List<int>());
}
foreach (int[] edge in edges){
graph[edge[0]].Add(edge[1]);
graph[edge[1]].Add(edge[0]);
}
int result = int.MinValue;
for (int i = 0; i < n; i++){
graph[i].Sort((x, y) => { return vals[x] - vals[y]; });
int length = graph[i].Count;
int ans = vals[i];
for (int j = length - 1; j >= Math.Max(0, length - k); j--){
if (vals[graph[i][j]] > 0) ans += vals[graph[i][j]];
}
result = Math.Max(result, ans);
}
return result;
}
}