-
Notifications
You must be signed in to change notification settings - Fork 6
/
js.js
46 lines (41 loc) · 1.11 KB
/
js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @param {number[]} vals
* @param {number[][]} edges
* @param {number} k
* @return {number}
*/
var maxStarSum = function (vals, edges, kMax) {
console.log('##########')
// console.log = () => {}
const graph = {}
vals.forEach((val, i) => {
graph[i] = {
val,
neighbours: new Set(),
}
})
edges.forEach(([a, b]) => {
graph[a].neighbours.add(b)
graph[b].neighbours.add(a)
})
let maxSum = vals[0]
Object.keys(graph).forEach(k => {
console.log('----')
const neighboursValsDesc = [...graph[k].neighbours]
.sort((a, b) => graph[b].val - graph[a].val)
.map(x => graph[x].val)
console.log(graph[k])
console.log(neighboursValsDesc)
let maxSumLocal = graph[k].val
for (let i = 0; i < neighboursValsDesc.length && i < kMax; i++) {
console.log('kMax', kMax, 'i', i)
maxSumLocal = Math.max(maxSumLocal, maxSumLocal + neighboursValsDesc[i])
console.log(maxSumLocal)
console.log('maxSumLocal', maxSumLocal)
}
maxSum = Math.max(maxSum, maxSumLocal)
console.log('maxSum', maxSum)
})
console.log(graph)
return maxSum
}