Untitled
unknown
plain_text
2 years ago
4.5 kB
6
Indexable
'use strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { inputString = inputString.trim().split('\n').map(string => { return string.replace(/\s+/g, " ").trim(); }); main(); }); function readLine() { return inputString[currentLine++]; } function readIntArr() { let str = readLine(); str = str.split(" "); let arr = []; for ( let i = 0; i < str.length; i++ ) { arr.push(parseInt(str[i], 10)); } return arr; } function print(x) { process.stdout.write(x + ""); } class Graph { constructor(v) { this.matrix = Array(v+1).fill().map(() => []) } addEdges(n1,n2,w) { this.matrix[n1].push([n2,w]) this.matrix[n2].push([n1,w]) } } class Item { constructor() { this.vertex; this.cost; } }; class PQ { constructor() { this.queue = [] this.size = -1 } enqueue (cost, vertex) { this.size++ this.queue[this.size] = new Item() this.queue[this.size].cost = cost this.queue[this.size].vertex = vertex } peek () { let lowest_cost = Number.MAX_SAFE_INTEGER let ind = -1 for (let i=0;i<=this.size;i++) { if (lowest_cost > this.queue[i].cost) { lowest_cost = this.queue[i].cost ind = i } } return ind } dequeue () { let ind = this.peek() let item = this.queue[ind] for (let i=ind;i<this.size;i++) this.queue[i] = this.queue[i+1] this.size-- return item } } /** * @param {number} n * @param {number[][]} edges * @return {number} */ function minCostOfRoad(n,edges) { // Create a graph using given edgelist let g = new Graph(n) for (let i=0;i<edges.length;i++) g.addEdges(...edges[i]) // Create an empty priority queue PQ. let pq = new PQ() // Initialize all vertices as not part of MST yet. let visited = Array(n+1).fill(false) // Insert source vertex into PQ and make its cost as 0. pq.enqueue(0, 1) // Keep a variable: totalCost and initialize it to 0 let totalCost = 0 while (pq.size >= 0) { let {cost, vertex} = pq.dequeue() if (!visited[vertex]) { visited[vertex] = true totalCost += cost for (let i=0;i<g.matrix[vertex].length;i++) { pq.enqueue(g.matrix[vertex][i][1], g.matrix[vertex][i][0]) } } } return totalCost } function main() { let [n,m] = readIntArr(); let edges = []; for (let i = 0; i < m; i++) { edges.push(readIntArr()); } let ans = minCostOfRoad(n, edges); console.log(ans); } /* Crio Methodology Milestone 1: Understand the problem clearly 1. Ask questions & clarify the problem statement clearly. 2. *Type down* an example or two to confirm your understanding of the input/output & extend it to test cases Milestone 2: Finalize approach & execution plan 1. Understand what type of problem you are solving. a. Obvious logic, tests ability to convert logic to code b. Figuring out logic c. Knowledge of specific domain or concepts d. Knowledge of specific algorithm e. Mapping real world into abstract concepts/data structures 2. Brainstorm multiple ways to solve the problem and pick one 3. Get to a point where you can explain your approach to a 10 year old 4. Take a stab at the high-level logic & *type it down*. 5. Try to offload processing to functions & keeping your main code small. Milestone 3: Code by expanding your pseudocode 1. Have frequent runs of your code, dont wait for the end 2. Make sure you name the variables, functions clearly. 3. Avoid constants in your code unless necessary; go for generic functions, you can use examples for your thinking though. 4. Use libraries as much as possible Milestone 4: Prove to the interviewer that your code works with unit tests 1. Make sure you check boundary conditions 2. Time & storage complexity 3. Suggest optimizations if applicable */
Editor is loading...