Maximum Star Sum of a Graph
Real-world version: Best Hub and Spokes YieldThe statement here is our own: the same inputs, outputs and technique.
A survey network has n sites numbered 0 through n - 1. Site i reports a yield of vals[i], which may be negative. edges[j] = [a, b] is a two-way cable between site a and site b.
A cluster is one site chosen as the hub, together with any set of at most k sites that share a cable with it. The hub is always part of its own cluster, and a cluster may consist of the hub alone. The yield of a cluster is the sum of the yields of its sites.
Return the largest yield any cluster can have.
Examples
Example 1
- Input
- vals = [2, -1, 5, -3], edges = [[0, 1], [0, 2], [1, 3]], k = 2
- Output
- 7
Taking site 0 as the hub and site 2 as its only spoke gives 2 + 5 = 7. Sites 0 and 2 do share a cable, and one spoke is within the allowance of two.
Example 2
- Input
- vals = [-5, -6, -7], edges = [[0, 1], [1, 2]], k = 2
- Output
- -5
Every yield is negative. A cluster of site 0 alone has yield -5, and no cluster does better.
Example 3
- Input
- vals = [10, 0, 0], edges = [[0, 1], [0, 2]], k = 2
- Output
- 10
Site 0 as the hub gives 10, and adding either zero-yield spoke leaves the total at 10, so 10 is the largest yield.
Constraints
- 1 <= vals.length <= 10^5
- -10^4 <= vals[i] <= 10^4
- 0 <= edges.length <= 10^5
- edges[j].length == 2
- 0 <= edges[j][0] <= 99999
- 0 <= edges[j][1] <= 99999
- A cable never joins a site to itself, and every site index is below vals.length
- 0 <= k <= 99999
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def best_hub_yield(vals: list[int], edges: list[list[int]], k: int) -> int:public int bestHubYield(int[] vals, int[][] edges, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Meeting Rooms IIReal-world version: Peak Charging Bay DemandMedium
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Number of Events That Can Be AttendedReal-world version: Most Inspections BookedMedium