문제



풀이
재영
성능 이슈가 있었…다…?
graph
를 객체로 만드는 순간, 자바스크립트에서 간선을 push
하는 부분이 현저히 성능이 떨어지는 이슈가 있었다.이를 3시간 디버깅한 결과… 결국 배열 자료구조로 바꾸니 매~우 빠르게 문제가 풀렸다. 😖
자바스크립트의 객체의 삽입과 수정은 굉장히 느리다는 것을 깨달았다.
가급적 배열을 이용하자.
class MinHeap { constructor() { this.heap = [null]; } heappush(value) { this.heap.push(value); let nowIndex = this.heap.length - 1; let parentIndex = Math.floor(nowIndex / 2); while (nowIndex > 1 && this.heap[parentIndex][1] > this.heap[nowIndex][1]) { this.swap(nowIndex, parentIndex); nowIndex = parentIndex; parentIndex = Math.floor(nowIndex / 2); } } heappop() { if (this.length === 1) return this.heap.pop(); const returnValue = this.heap[1]; this.heap[1] = this.heap.pop(); let nowIndex = 1; let leftIndex = nowIndex * 2; let rightIndex = nowIndex * 2 + 1; if (!this.heap[rightIndex]) { if ( this.heap[leftIndex] && this.heap[nowIndex][1] > this.heap[leftIndex][1] ) { this.swap(nowIndex, leftIndex); return returnValue; } } while ( this.heap[rightIndex] && (this.heap[nowIndex][1] > this.heap[leftIndex][1] || this.heap[nowIndex][1] > this.heap[rightIndex][1]) ) { if (this.heap[leftIndex][1] < this.heap[rightIndex][1]) { this.swap(nowIndex, leftIndex); nowIndex = leftIndex; } else { this.swap(nowIndex, rightIndex); nowIndex = rightIndex; } leftIndex = nowIndex * 2; rightIndex = nowIndex * 2 + 1; } return returnValue; } swap(a, b) { [this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]]; } get length() { return this.heap.length - 1; } } const solution = (n, paths, gates, summits) => { const minHeap = new MinHeap(); const graph = Array.from({ length: n + 1 }, () => []); paths.forEach(([a, b, c]) => { graph[a].push([b, c]) graph[b].push([a, c]) }) const set = new Set(summits); gates.forEach((gate) => { minHeap.heappush([gate, 0]); }); const dist = new Array(n + 1).fill(10000001); while (minHeap.length) { const [now, weight] = minHeap.heappop(); if (dist[now] <= weight) continue; dist[now] = weight; if (set.has(now)) { continue; } set.add(now) if (graph[now]) { for (const [nxt, nxtWeight] of graph[now]) { const diff = Math.max(nxtWeight, weight); if (diff < dist[nxt]) { minHeap.heappush([nxt, diff]); } } } } const result = [-1, 10000001]; summits.sort((a, b) => a - b).forEach((v) => { if (dist[v] < result[1]) { result[0] = v; result[1] = dist[v]; } }); return result; };