|
| 1 | +//https://github.com/codeisneverodd/programmers-coding-test |
| 2 | +//완벽한 정답이 아닙니다. |
| 3 | + |
1 | 4 | //정답 1 - pereng11
|
2 | 5 | // 다익스트라 + 최소힙 O( N * logN )
|
3 |
| -function solution ( N, road, K ) |
4 |
| -{ |
5 | 6 | // [목적지, 거리] 노드를 값으로 가지는, 거리에 대한 최소힙
|
6 |
| - class MinHeap{ |
7 |
| - constructor () |
8 |
| - { |
9 |
| - this.heap = [ null ]; |
10 |
| - } |
11 |
| - // 맨 끝에 노드를 삽입 후 위로 올라가면서 정렬 |
12 |
| - push ( val ) |
| 7 | +class MinHeap{ |
| 8 | + constructor () |
| 9 | + { |
| 10 | + this.heap = [ null ]; |
| 11 | + } |
| 12 | + // 맨 끝에 노드를 삽입 후 위로 올라가면서 정렬 |
| 13 | + push ( val ) |
| 14 | + { |
| 15 | + this.heap.push(val); |
| 16 | + let childIdx = this.heap.length-1; |
| 17 | + let parentIdx = Math.floor(childIdx / 2); |
| 18 | + while(parentIdx > 0 && this.heap[parentIdx][1] > this.heap[childIdx][1]){ |
| 19 | + this.swap( childIdx, parentIdx ); |
| 20 | + childIdx = parentIdx; |
| 21 | + parentIdx = Math.floor(childIdx / 2); |
| 22 | + } |
| 23 | + } |
| 24 | + pop () |
| 25 | + { |
| 26 | + if ( this.heap.length === 1 ) |
13 | 27 | {
|
14 |
| - this.heap.push(val); |
15 |
| - let childIdx = this.heap.length-1; |
16 |
| - let parentIdx = Math.floor(childIdx / 2); |
17 |
| - while(parentIdx > 0 && this.heap[parentIdx][1] > this.heap[childIdx][1]){ |
18 |
| - this.swap( childIdx, parentIdx ); |
19 |
| - childIdx = parentIdx; |
20 |
| - parentIdx = Math.floor(childIdx / 2); |
21 |
| - } |
| 28 | + return undefined; |
22 | 29 | }
|
23 |
| - pop () |
| 30 | + // 최소값은 빼두었다가 리턴하고, 가장 끝 값을 맨 위로 가져와 아래로 내려가면서 정렬 |
| 31 | + const minNode = this.heap[ 1 ]; |
| 32 | + this.heap[ 1 ] = this.heap[ this.heap.length - 1 ]; |
| 33 | + this.heap.pop(); |
| 34 | + let parentIdx = 1; |
| 35 | + let leftChildIdx = 2; |
| 36 | + let rightChildIdx = 3; |
| 37 | + while ( parentIdx < this.heap.length ) |
24 | 38 | {
|
25 |
| - if ( this.heap.length === 1 ) |
| 39 | + // 자식이 없는 경우 |
| 40 | + if ( !this.heap[ leftChildIdx ] ) |
26 | 41 | {
|
27 |
| - return undefined; |
28 |
| - } |
29 |
| - // 최소값은 빼두었다가 리턴하고, 가장 끝 값을 맨 위로 가져와 아래로 내려가면서 정렬 |
30 |
| - const minNode = this.heap[ 1 ]; |
31 |
| - this.heap[ 1 ] = this.heap[ this.heap.length - 1 ]; |
32 |
| - this.heap.pop(); |
33 |
| - let parentIdx = 1; |
34 |
| - let leftChildIdx = 2; |
35 |
| - let rightChildIdx = 3; |
36 |
| - while ( parentIdx < this.heap.length ) |
| 42 | + break; |
| 43 | + } // 왼쪽 자식만 있는 경우 |
| 44 | + else if ( !this.heap[ rightChildIdx ] ) |
37 | 45 | {
|
38 |
| - // 자식이 없는 경우 |
39 |
| - if ( !this.heap[ leftChildIdx ] ) |
40 |
| - { |
41 |
| - break; |
42 |
| - } // 왼쪽 자식만 있는 경우 |
43 |
| - else if ( !this.heap[ rightChildIdx ] ) |
44 |
| - { |
45 |
| - if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] ) |
46 |
| - { |
47 |
| - this.swap( parentIdx, leftChildIdx ); |
48 |
| - } |
49 |
| - break; |
50 |
| - // 둘 중 하나가 부모보다 작을 때, 더 작은 쪽으로 정렬 |
51 |
| - } else if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] || this.heap[ parentIdx ][ 1 ] > this.heap[ rightChildIdx ][ 1 ] ) |
52 |
| - { |
53 |
| - const minChildIdx = this.heap[ leftChildIdx ][ 1 ] < this.heap[ rightChildIdx ][ 1 ] ? leftChildIdx : rightChildIdx; |
54 |
| - this.swap( parentIdx, minChildIdx ); |
55 |
| - parentIdx = minChildIdx; |
56 |
| - leftChildIdx = parentIdx * 2 |
57 |
| - rightChildIdx = parentIdx * 2 + 1; |
58 |
| - } else |
| 46 | + if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] ) |
59 | 47 | {
|
60 |
| - // 끝까지 내려가지 않았더라도 부모가 가장 작으면 정렬 중지 |
61 |
| - break; |
| 48 | + this.swap( parentIdx, leftChildIdx ); |
62 | 49 | }
|
| 50 | + break; |
| 51 | + // 둘 중 하나가 부모보다 작을 때, 더 작은 쪽으로 정렬 |
| 52 | + } else if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] || this.heap[ parentIdx ][ 1 ] > this.heap[ rightChildIdx ][ 1 ] ) |
| 53 | + { |
| 54 | + const minChildIdx = this.heap[ leftChildIdx ][ 1 ] < this.heap[ rightChildIdx ][ 1 ] ? leftChildIdx : rightChildIdx; |
| 55 | + this.swap( parentIdx, minChildIdx ); |
| 56 | + parentIdx = minChildIdx; |
| 57 | + leftChildIdx = parentIdx * 2 |
| 58 | + rightChildIdx = parentIdx * 2 + 1; |
| 59 | + } else |
| 60 | + { |
| 61 | + // 끝까지 내려가지 않았더라도 부모가 가장 작으면 정렬 중지 |
| 62 | + break; |
63 | 63 | }
|
64 |
| - return minNode; |
65 | 64 | }
|
66 |
| - swap ( idx1, idx2 ) |
67 |
| - { |
68 |
| - [ this.heap[ idx1 ], this.heap[ idx2 ] ] = [ this.heap[ idx2 ], this.heap[ idx1 ] ]; |
69 |
| - } |
70 |
| - length () |
71 |
| - { |
72 |
| - return this.heap.length; |
73 |
| - } |
74 |
| - } |
| 65 | + return minNode; |
| 66 | + } |
| 67 | + swap ( idx1, idx2 ) |
| 68 | + { |
| 69 | + [ this.heap[ idx1 ], this.heap[ idx2 ] ] = [ this.heap[ idx2 ], this.heap[ idx1 ] ]; |
| 70 | + } |
| 71 | + length () |
| 72 | + { |
| 73 | + return this.heap.length; |
| 74 | + } |
| 75 | +} |
75 | 76 |
|
76 |
| - |
| 77 | +function solution ( N, road, K ) |
| 78 | +{ |
77 | 79 | const roadsTable = {}; //전체 도로 정보
|
78 | 80 |
|
79 | 81 | // 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열
|
@@ -136,7 +138,6 @@ function solution ( N, road, K )
|
136 | 138 | //다익스트라 + 선형탐색
|
137 | 139 | function solution ( N, road, K )
|
138 | 140 | {
|
139 |
| - |
140 | 141 | const roadsTable = {}; //전체 도로 정보
|
141 | 142 |
|
142 | 143 | // 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열
|
|
0 commit comments