MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / buildGraph

Function buildGraph

javascript/1584-Min-Cost-to-Connect-all-Points.js:22–40  ·  view source on GitHub ↗
(points)

Source from the content-addressed store, hash-verified

20})
21
22const buildGraph = (points) => {
23 const { graph, seen, minHeap } = initGraph(points);
24
25 for (let src = 0; src < (points.length - 1); src++) {
26 for (let dst = (src + 1); (dst < points.length); dst++) {
27 const cost = getCost(points, src, dst);
28
29 graph[src].push([ dst, cost ]);
30 graph[dst].push([ src, cost ]);
31 }
32 }
33
34 const [ src, cost, priority ] = [ 0, 0, 0 ];
35 const node = [ src, cost ];
36
37 minHeap.enqueue(node, priority);
38
39 return { graph, seen, minHeap };
40}
41
42const getCost = (points, src, dst) => {
43 const [ [ x1, y1 ], [ x2, y2 ] ] = [ points[src], points[dst] ];

Callers 1

minCostConnectPointsFunction · 0.70

Calls 3

initGraphFunction · 0.70
getCostFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected