There is an undirected graph consisting of n nodes numbered from 0 to n - 1. You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node.
You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.
A star graph is a subgraph of the given graph having a center node containing 0 or `more neighbors. In other words, it is a subset of edges of the given graph such that there exists a common node for all edges.
The image below shows star graphs with 3 and 4 neighbors respectively, centered at the blue node.
graph TD;
subgraph S1[" "]
A(" ") --- B(" ") & C(" ") & D(" ")
end
subgraph S2[" "]
E(" ") --- F(" ") --- G(" ")
H(" ") --- F
F --- I(" ")
end
classDef blue fill:#87CEEB,stroke:#333,stroke-width:2px;
class A blue
class F blue
The star sum is the sum of the values of all the nodes present in the star graph.
Given an integer k, return the maximum star sum of a star graph containing at mostkedges.
Input:
vals =[1,2,3,4,10,-10,-20], edges =[[0,1],[1,2],[1,3],[3,4],[3,5],[3,6]], k =2Output:
16Explanation: The above diagram represents the input graph.The star graph with the maximum star sum is denoted by blue. It is centered at 3 and includes its neighbors 1 and 4.It can be shown it is not possible to get a star graph with a sum greater than 16.
Example 2:
graph TD
A0(num: 0 val: -5)
1
2
3
4
5
6
Input:
vals =[-5], edges =[], k =0Output:
-5Explanation: There is only one possible star graph, which is node 0 itself.Hence, we return-5.
For each node, the best way to maximize the star sum is to select up to k neighbors with the largest positive values. We use a max-heap (priority queue) to efficiently pick the top k neighbors for each node.
classSolution {
public:int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {
int n = vals.size(), ans = INT_MIN;
vector<vector<int>> g(n);
for (auto& e : edges) {
g[e[0]].push_back(e[1]);
g[e[1]].push_back(e[0]);
}
for (int i =0; i < n; ++i) {
priority_queue<int> pq;
for (int v : g[i]) pq.push(vals[v]);
int sum = vals[i], cnt =0;
while (!pq.empty() && cnt < k && pq.top() >0) {
sum += pq.top(); pq.pop(); ++cnt;
}
ans = max(ans, sum);
}
return ans;
}
};
classSolution {
publicintmaxStarSum(int[] vals, int[][] edges, int k) {
int n = vals.length, ans = Integer.MIN_VALUE;
List<List<Integer>> g =new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
for (int[] e : edges) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
for (int i = 0; i < n; i++) {
PriorityQueue<Integer> pq =new PriorityQueue<>(Collections.reverseOrder());
for (int v : g.get(i)) pq.offer(vals[v]);
int sum = vals[i], cnt = 0;
while (!pq.isEmpty() && cnt < k && pq.peek() > 0) {
sum += pq.poll(); cnt++;
}
ans = Math.max(ans, sum);
}
return ans;
}
}
classSolution {
funmaxStarSum(vals: IntArray, edges: Array<IntArray>, k: Int): Int {
val n = vals.size
val g = List(n) { mutableListOf<Int>() }
for (e in edges) {
g[e[0]].add(e[1])
g[e[1]].add(e[0])
}
var ans = vals[0]
for (i in0 until n) {
val neighbors = g[i].map { vals[it] }.sortedDescending()
var sum = vals[i]
for (j in0 until minOf(k, neighbors.size)) {
if (neighbors[j] > 0) sum += neighbors[j] elsebreak }
ans = maxOf(ans, sum)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defmaxStarSum(self, vals: list[int], edges: list[list[int]], k: int) -> int:
n = len(vals)
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
ans = vals[0]
for i in range(n):
neighbors = sorted([vals[v] for v in g[i]], reverse=True)
s = vals[i]
for j in range(min(k, len(neighbors))):
if neighbors[j] >0:
s += neighbors[j]
else:
break ans = max(ans, s)
return ans
⏰ Time complexity: O(n + m + n k log k), where n is the number of nodes, m is the number of edges. For each node, we sort or heapify up to k neighbors.
🧺 Space complexity: O(n + m), for the adjacency list and temporary storage.