Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function maxStarSum(vals: number[], edges: number[][], k: number): number {
- const links = new Map<number, number[]>();
- function add(i1: number, i2: number) {
- if (links.has(i1)) {
- links.get(i1).push(vals[i2]);
- }
- else {
- links.set(i1, [vals[i2]]);
- }
- }
- for(const edge of edges) {
- const [i1, i2] = edge;
- add(i1, i2);
- add(i2, i1);
- }
- let max = -10000 * (k+2);
- for(let i=0; i<vals.length; i++) {
- const thisLinks = links.get(i) ?? [];
- // take the best K links, but don't take any negative numbers
- const sum = thisLinks.sort((a,b) => b-a).slice(0,k).filter(i => i>0)
- // add the center node (it's not optional)
- .concat([vals[i]])
- // sum
- .reduce((a,b) => a + b, 0);
- if (sum > max) max = sum;
- }
- return max;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement